Deleting files using a service application

I am using FileSystemWatcher in VS2005 to monitor directories and was thinking of changing my application to a service. Eveverything woks fine until the service tries to delete the file. By sending an exception to eventviewer, I am getting System.UnauthorizedAccessException. This file (testing) can be deleted by the same FileSystemWatcher code when run on a form. I checked the LocalService permissions on the folder and even changed who started the service. I have commented out the FileSystemWatcher file code and still cannot delete files with the service I am trying to use File.Delete (filename). Thinking that the icon might be with "File" I tried to create File.Copy (filename, newfilename) and it works great

+1


source to share


2 answers


Try using the regular old user account for the service instead of using LocalSystem. This way you can grant certain permissions to the service and prevent it from accessing all the things that LocalSystem does.



Also, I found the FileSystemWatcher hurts more than its good. Often, it detects the existence of a file before it is completely written, and this causes all sorts of problems when working with the file. Try polling (check the folder every few seconds - don't use the Timer class - its own problems !) For new files.

+1


source


"who started the service has changed" Was whoever started the service or the account configured for the service in ??



In general, when a service has permissions issues that it does not get when it starts up as a default application, the problem is with the service account set up permanently.

0


source







All Articles