views:

102

answers:

2

I am using a FileSystemWatcher in VS2005 to monitor directories and thought to change the application to a service. Eveverything woks fine until the service tries to delete a file. Sending the exception to the eventviewer, I get a System.UnauthorizedAccessException. The file in question (testing) can be deleted by the same FileSystemWatcher code when run in a form. I have checked the LocalService rights to the folder and even changed who ran the service. I commented out the code about the FileSystemWatcher and still cannot delete files using the service I am attempting to use File.Delete(filename). Thinking the propblem might be with "File", I attempted a File.Copy(filename, newfilename) and that works fine

+1  A: 

Try using a plain old user account for the service instead of using LocalSystem. This way you can grant specific permissions for the service and not let it have access to all the things that LocalSystem does.

Also, I found that FileSystemWatcher is a more of a pain than its good for. Often it will detect the presence of a file before its completely written, and this causes all kinds of issues when working with the file. Try using a polling (check the folder every few seconds - Do not use the Timer class - its got issues of its own!) for new files.

StingyJack
A: 

"changed who ran the service" Was that who started the service or the account the service was configured to run under?

In general, when a service has rights problems it doesn't get when run as a standard app, the problem is invariably the service's configured account.

cookre