https://blog.csdn.net/qq_38061677/article/details/81157116

代码:

using System;
namespace Project2048
{
class Program
{
static void Main(string[] args)
{
FileInfo fileInfo1 = new FileInfo("555.txt");//该文件是存在的
string str = "hello";
File.AppendAllText(fileInfo1.DirectoryName, str);
Console.ReadKey();
}

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
System.UnauthorizedAccessException解析
在VS默认的解释是: path 指定了一个只读文件。- 或 -在当前平台上不支持此操作。- 或 -path 指定了一个目录。- 或 -调用方没有所要求的权限。
疑惑

FIleInfo类实例化,默认的的只读属性是false,也就是说。可以进行读写的,怎么会出System.UnauthorizedAccessException”错误?

经过仔细寻找发现问题是fileInfo1.DirectoryName,这个方法是获取文件的目录,并不是到当前文件。使用fileInfo.Name就可以了
代码:

using System;
namespace Project2048
{
class Program
{
static void Main(string[] args)
{
FileInfo fileInfo1 = new FileInfo("555.txt");//该文件是存在的
string str = "hello";
File.AppendAllText(fileInfo1.Name, str);
Console.ReadKey();
}

}
}
---------------------
作者:小小liang
来源:CSDN
原文:https://blog.csdn.net/qq_38061677/article/details/81157116
版权声明:本文为博主原创文章,转载请附上博文链接!

相关文章:

  • 2021-10-06
  • 2022-01-29
  • 2022-12-23
  • 2021-05-19
  • 2022-12-23
  • 2021-12-02
  • 2021-07-04
  • 2022-12-23
猜你喜欢
  • 2021-09-02
  • 2021-06-28
  • 2022-12-23
  • 2022-12-23
  • 1970-01-01
  • 2022-12-23
  • 2021-05-21
相关资源
相似解决方案