FileSystemObject in a windows service

biosninja

New member
Joined
Jan 26, 2007
Messages
4
Programming Experience
3-5
Hi.

First off... I'm new to the board....so hello everyone....

i have a weird problem with a windows service i'm writing....

The service is upposed the scan a specified directory for any PDF files and import them into our system using the ssytem API.

Now when the service starts and i view my log file, it says, service starting, service started and nothing afterwards, it is suposed to have scanned for the files, but nothing has happened.

I even moved the code to the OnStart sub, but still nothing.

here is the code i used : (the scanning of files was in the StartThread.Start part of the code)

VB.NET:
[SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] OnStart([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] args() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#008000]' Add code here to start your service. This method should set things[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]' in motion so your service can do its work.[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]' Add code here to start your service. This method should set things[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]' in motion so your service can do its work.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] fso [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Scripting.FileSystemObject[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] rfldr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Scripting.Folder[/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] f [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Scripting.File[/SIZE]
[SIZE=2]getRegValues()[/SIZE]
[SIZE=2][COLOR=#008000]'rfldr = fso.GetFolder(path)[/COLOR][/SIZE]
[SIZE=2]rfldr = fso.GetFolder("c:\")[/SIZE]
[SIZE=2]WriteMessage("test: " + rfldr.Files.Count(), "aw")[/SIZE]
[SIZE=2]WriteMessage(" Starting Service", "AwardsAgent")[/SIZE]
[SIZE=2]StartThread.Start()[/SIZE]
[SIZE=2]WriteMessage(" Service Started", "AwardsAgent")[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]


I added the scripting.runtime component for this.

Now when I start my service, I get the following message: "The AwardsAgent service in Local Computer started and then stopped. Some services stop automatically if they have no work to do, for example, the Performance Logs and Alerts service"

I then made the service into an normal application using a windows form instead of the service, and the application works perfectly!
 
Last edited:
I just tried with a Windows Service (which I don't do often!) and got the same error. It looks like this was a problem with the EventLog and service account LocalService, at some point (really don't know what code I changed..) I got a log in event for security exception because the SourceExists checked all logs and didn't have access to the Security log under that account. Changing to account LocalSystem made it work. This is something to consider, with service security error it may stop without any other notice than the one you got.

About FileSystemObject, you don't need it. .Net Framework have that covered in System.IO namespace with Directory class and DirectoryInfo class.
 
thanks for the help....the service already starts using the LocalSystem account..

I will try to use the system.io namespace and see waht happens...

thanks


'UPDATE

I fixed this issue.....

i replaced the fso objects with the system.io namespace....thats was all ok, but it looks like VB.Net does not like mapped network drives.

I used "c:\" as the source directory and it worked. Then I tried "h:\" wich is the mapped network drive and nothing happens. the I used the direct network path ie. "\\servername\dir" and this worked....so yeah..problem solved

Thanks all the help guys...
 
Last edited:
Mapped drive letters are only valid for the user that have them mapped. A Windows Service does not operate in user context, but in its own account context like LocalSystem, LocalService.. even when running under a local user account it is not the same as desktop user. That is the explanation. There is no problem with VB.Net and mapped network drives in general.
 
i mapped the network drive using a certain network user acount (the same that the server logs in with) and started the service with that same userid...shouldnt that be suffiencient?

or is there just no way to use a mapped drive in a .Net service?
 
Back
Top