khcha22
khcha22

Reputation: 77

Error when opening file for reading in c#

This code works in a normal project.

string path = @"...\My_App_Data\Sample.xml";
Using(FileStream fs = File.Open(path,FileMode.Open, FileAccess.Read, FileShare.Read))
{

}

But, when i put the same code into a method in a class library and then call the method, i am getting UnAuthorizedAccessException. NOTE: The Sample.xml is in a My_App_Data folder in the solution explorer.

Even if i open the VS.net with admin rights, i get the same error.

Can anyone resolve this. Please help..

Upvotes: 2

Views: 201

Answers (1)

Zach Leighton
Zach Leighton

Reputation: 1941

This is running in the bin/debug folder so you need to find the path relative to that.

If you made a folder in your root solution it would (possibly) be:

@"..\\..\\..\\My_App_Data\\Sample.xml"

Basically.. up from debug \ up from bin \ up from project folder \My_App_Data\Sample.xml

I also escaped the \

Upvotes: 2

Related Questions