为什么访问路径被拒绝?
问题描述:
我想用File.ReadAllText和FileStream读取一个文本文件,但由于某种原因我每次都得到System.UnauthorizedAccessException。为什么访问路径被拒绝?
class consultas
{
public consultas()
{
}
private string Inativos = @"C:\Users\Mathias Cruz\Desktop\helloWorld\helloWorld\Consultas";
public string getInativos()
{
try
{
// string path = Directory.GetCurrentDirectory();
this.Inativos = File.ReadAllText(this.Inativos);
}
catch(Exception e)
{
throw e;
}
return this.Inativos;
}
}
为什么?我在该文件夹中有权限,那么为什么我会得到这个异常?
答
基于您的代码,你要么试图读取文件夹,因为你没有在你的文件路径指定扩展这里:
private string Inativos = @"C:\Users\Mathias Cruz\Desktop\helloWorld\helloWorld\Consultas";
它肯定会抛出一个UnauthorizedAccessException错误。所以确保你有确切的文件路径及其扩展名。
+0
谢谢你,我忘了这个细节kkkkkkkk再次感谢。 –
答
因为path是一个directory.please检查你的文件地址。 此方法需要文件地址。 File.ReadAllText(“C:\ yourfile.txt”);
给这个帖子试一试:http://stackoverflow.com/questions/4877741/access-to-the-path-is-denied – pasotee