Give Windows Service permissions to update a file on the hard drive when the user is logged out, using C#
quarta-feira, 7 de março de 2012 10:50
I have a C# application which runs while a user is logged in, and then starts an associated Windows Service (again in C#) when the user logs out. While the user is logged in, the application is able to write to a file location on the local hard drive - this is created to hold information, and needs to be updated by the application (file is @"C:\MyApp\App_Files\Info.xml"). However, when the user logs out and the Windows Service tries to update this same file, it can't due to lack of permissions.
This may be easier that I thought, but I have been searching this and can't seem to find a way to do this. By the way, the service can read from the file fine, and if I specify a path in the MyDocuments folder (e.g. Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + @"\MyApp\App_Files\Info.xml", then it is able to update the file, but I need to use this file path on the hard disk.
So, overall, how do I give permissions to allow a Windows Service to update a file in a specific path location?
Todas as Respostas
quarta-feira, 7 de março de 2012 12:25
quarta-feira, 7 de março de 2012 13:43
do the following,
Click start-> run-> services.msc on the services snap-in find your service and right click it->properties->select Log On tab-> select This Account and set the user account.
quinta-feira, 8 de março de 2012 10:50
Hi Paulo and Bilhan,
Paulo - the Windows Service is there when the user is logged in but stopped. Its only when the user clicks Logout that the application recognises this is happening and starts the Windows Service.
Bilhan - I need this to happen automatically i.e. without user action on each machine. So, my question is how do I ensure that the Windows Service has permissions to modify this file by itself? Alternatively, can this be done at install i.e. when the application is installed it adds the Windows Service also, but this is only started when the user logs out?
quinta-feira, 8 de março de 2012 11:37
To know when user logs out you can use ManagementEventWatcher class and subscribe to EventArrived event. When user logs off windows writes an event entry. To know if you have file access writes you can use the FileIOPermission class with SecurityManager.IsGranted method.
You can use a setup project to install windows service.
ps: You can set your service to run a System so it will have access rights to entire system. First your should add a service process installer and a service installer. check here. then select the service process installer and set on properties window set the Account to LocalSystem.
quinta-feira, 29 de março de 2012 11:52
Apologies for not getting back to you sooner, and thanks for your update.
I use SystemEvents to detect if the user logs out, and if so then start the Windows Service (similar to http://codeidol.com/csharp/csharpckbk2/Toolbox/Dealing-with-Operating-System-Shutdown,-Power-Management,-or-User-Session-Changes). Overall, did you find whether ManagementEventWatcher is a better way of detecting events when they happen (logoff, shutdown, etc)?
Back to the main point, I can see the service process installer properties (which includes Name, AutoLog, CanHandlePowerEvent, ... ServiceName etc), but can't see how to set the Account to LocalSystem on the properties window.