我在使用以下代码创建 .txt 文件时遇到问题。我得到如下异常:
Unhandled exception: System.unauthorizedAccessException: Access to the path 'C:\log.txt' is denied.
我在网上看过并做了与 api 上的类似的事情。下面是我的代码,所以你可以理解我的逻辑序列是什么。你认为是什么导致了这个异常?提前致谢!
static StreamWriter swt;
static string logFile = @"C:\log.txt";
static FileStream fs;
static void Main(string[] args)
{
Console.Out.WriteLine(swt);
string root = args[0];
if (!File.Exists(logFile))
{
try
{
fs = File.Create(logFile);
}
catch (Exception ex)
{
swt.WriteLine("EXCEPTION HAS BEEN THROWN:\n " + ex + "\n");
}
{
}
}
}
最佳答案
您很可能会收到此错误,因为标准用户无法在没有提升权限的情况下写入驱动器的根目录。参见 here .
关于C#,创建 .txt 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16897395/