views:

50

answers:

2

Hi guys,

I have deployed successfully a C# windows 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

A: 

You're using String.Format() in the wrong way. Look at msdn.

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);
brickner
`Path.Combine`.
SLaks
Without knowing what is stored in "folderName" we can not say that the usage is wrong. eg: string folderName = "c:\user\{0}";\\ would work perfectly fine.
Amby
@Amby, that's not a reasonable usage for a variable with that name.
brickner
Arghhhhh! I accidently used Format instead of combined.Works well now. Thanks for the help.
Joseph Ghassan
@brickner, I agree. In that case we can even argue a variable name like "this.serviceProcessInstaller1".
Amby
A: 

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);
Amby
I used Path.Combine() as suggested above. Thanks.
Joseph Ghassan