VS 2019生成的新项目无法读取或写入文件。甚至下面的简单代码也不起作用。
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("hello");
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
var folder = Path.GetDirectoryName(path);
var filePath = Path.Combine(folder, "test.txt");
Console.WriteLine(filePath);
// File.WriteAllLines(filePath, new[] { "hello" });
using (var f = new FileStream(filePath, FileMode.OpenOrCreate))
{
f.Write(new byte[] { 0x39 }, 0, 1);
}
}
}
使用访问文件
给出例外
Unhandled exception. System.UnauthorizedAccessException: Access to the path 'C:\Users\user\Documents\ConsoleApp1\ConsoleApp1\Debug\netcoreapp3.1\test.txt' is denied.
但是,它可以在.Net Core 2.2或更低版本中使用,也可以使用dotnet ConsoleApp1.dll
。
我其他几年的项目都在工作。我以前从未遇到过这样的问题。有什么问题以及如何解决?
原来我的防病毒软件阻止了文件访问。第一天没有弹出窗口,所以我不知道。后来,终于有了弹出窗口。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句