C# Windows Service Access denied when trying to write in a folder
I have deployed successfully a C# win开发者_StackOverflowdows service on a windows 7 machine.
Now, when I try to create a file using this code :
FileStream os = new FileStream(String.Format(folderName, fileName), FileMode.Create);
I get Access to filepath is denied.
In the service Installer I set the following parameters to :
this.serviceProcessInstaller1.Account =
System.ServiceProcess.ServiceAccount.LocalSystem;
this.serviceProcessInstaller1.Password = "Pass";
this.serviceProcessInstaller1.Username = "Administrator"
I added all the possible accounts with Full permissions to the folder where I want to create the file but nothing helped.
Any suggestions would be highly appreciated
You're using String.Format()
in the wrong way. Look at msdn http://msdn.microsoft.com/en-us/library/fht0f5be.aspx.
You probably want something like
FileStream os = new FileStream(folderName +@"\" + fileName, FileMode.Create);
or
FileStream os = new FileStream(String.Format(@"{0}\{1}", folderName fileName), FileMode.Create);
or
FileStream os = new FileStream(Path.Combine(folderName fileName), FileMode.Create);
May not be the answer. But to start with, log the exact path that the service is trying to get access to. Then, by using the credentials that you have provided to your service log-on to the machine and try to access that path.
string fullPath = String.Format(folderName, fileName);
logger.Write(fullPath);
FileStream os = new FileStream(fullPath, FileMode.Create);
精彩评论