阅读在C#错误打开文件时 [英] Error when opening file for reading in c#

查看:136
本文介绍了阅读在C#错误打开文件时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这code工作在一个正常的项目。

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))
{

}

但是,当我把同样的code变成了一种方法,一个类库,然后调用方法,我收到UnAuthorizedAccessException。 注:sample.xml的是在解决方案资源管理My_App_Data文件夹

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.

即使我打开VS.net管理员权限,我得到了同样的错误。

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

任何人都可以解决这个问题。请帮助..

Can anyone resolve this. Please help..

推荐答案

这是在bin / debug文件夹中运行,所以你需要找到相对的路径。

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的

基本上.. 从调试\从斌\从项目文件夹\ My_App_Data \ sample.xml中

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

我也逃过了\

这篇关于阅读在C#错误打开文件时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆