Reputation: 949
I am trying to open a file (included in my project as Content and Copy Always options) using a FileStream. I am getting the following error:
***Access to the path 'E:\approot\PdataParsingRules.xml is denied.***
I am using the below code to get the path of my file:
Path.Combine(Environment.GetEnvironmentVariable("RoleRoot") + @"\", @"approot\PdataParsingRules.xml");
And I am using the below code to de serialize my file:
ParsingRules rules;
XmlSerializer serializer = new XmlSerializer(typeof(ParsingRules));
fileStream = new FileStream(rulePath, FileMode.Open);
rules = (ParsingRules)serializer.Deserialize(fileStream);
return rules;
When I do an RDC to one of my worker role instances (running in full trust mode), I see that this particular file has Read, Read & Execute rights for regular users in that VM. Admin and System have full control on the file. My de serialization works fine if I manually give full rights to regular users but that doesn't solve the problem for obvious reasons.
Any ideas on this would be greatly appreciated.
Upvotes: 2
Views: 2388
Reputation: 30903
Looking at the documentation for the constructor you are using I see the following:
For constructors without a FileAccess parameter, if the mode parameter is set to Append, Write is the default access. Otherwise, the access is set to ReadWrite.
And by default, if you do not run your role with elevated permissions your code does not have WRITE access to the files. You can only read them. Please try using constructor that also specifies the FileAccess mode like this:
fileStream = new FileStream(rulePath, FileMode.Open, FileAccess.Read);
I think this is the key to your issue.
** EDIT **
Now that I tested using both constructors I can confirm that this (what I described) has been your issue. If you want to just read your file, use the constructor I am refering (including FileAccess parameter). If you want to also write to your file, you have to include a startup task to change file permissions.
If you are for the latter, this thread might be in real help!
Upvotes: 3