Error when opening file for reading in c#

空扰寡人 提交于 2019-12-24 03:59:21

问题


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..


回答1:


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 \



来源:https://stackoverflow.com/questions/30514462/error-when-opening-file-for-reading-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!