https://blog.csdn.net/qq_38061677/article/details/81157116spa
代码:.net
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();
}blog
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
System.UnauthorizedAccessException解析
在VS默认的解释是: path 指定了一个只读文件。- 或 -在当前平台上不支持此操做。- 或 -path 指定了一个目录。- 或 -调用方没有所要求的权限。
疑惑string
FIleInfo类实例化,默认的的只读属性是false,也就是说。能够进行读写的,怎么会出System.UnauthorizedAccessException”错误?io
通过仔细寻找发现问题是fileInfo1.DirectoryName,这个方法是获取文件的目录,并非到当前文件。使用fileInfo.Name就能够了
代码:class
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();
}file
}
}
---------------------
做者:小小liang
来源:CSDN
原文:https://blog.csdn.net/qq_38061677/article/details/81157116
版权声明:本文为博主原创文章,转载请附上博文连接!权限