Windows Service - Cannot access config file.

Feb 4 2011 9:49 AM

Hello,

I've recently created a Windows Service(C#) which does exactly what I want it to do, however. Upon putting certain settings into a configuration file so the Server Manager can change its settings when required, the service cannot access the file.

I've tried the following without success:

  • Setting the Service to install as Local System
  • Setting the Permissions on the top level directory to read and write for everyone
  • Hard Coding the path in the Windows Service
  • Not being greedy in the FileStream and only specifying FileAccess.Read

I'm really not sure what else i can try. The FileStream line of code is below:

  FileStream stream = new FileStream(Path.GetDirectoryName(Path.Combine(dir, @"\Config\Settings.xml")), FileMode.Open, FileAccess.Read);

The error message I get is as follows:

Service cannot be started. System.UnauthorizedAccessException: Access to the path 'C:\Ultilearn Account Listener\Config' is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
at AccountListener.AccountListener.GetNodeValue(String path)
at AccountListener.AccountListener.GetSettings()
at AccountListener.AccountListener.OnStart(String[] args)
at System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object state)

Thanks in advance for any and all help.


Answers (2)