C#,创建.txt文件 [英] C#, Creating .txt file
问题描述
我无法建立与下面的代码.txt文件。我得到一个异常如下:
未处理的异常:System.UnauthorizedAccessException:对在
路径C:\log .TXT被拒绝。
块引用>
我在网上看了一下,做了类似的事情,什么是对的API。下面是我的代码,这样你就可以了解我的逻辑的思路是什么。你觉得原因是什么这个例外? !在此先感谢
静态的StreamWriter SWT;
静态字符串日志文件= @C:\log.txt
静态的FileStream FS;
静态无效的主要(字串[] args)
{
Console.Out.WriteLine(SWT);
串根= ARGS [0]; (!File.Exists(日志))
如果
{
试
{
FS = File.Create(日志文件);
}
赶上(异常前)
{
swt.WriteLine(例外抛出:\\\
+前+\\\
);
}
{
}
}
}
解决方案您是最有可能得到这个错误,因为标准用户不能写入到驱动器的没有提升的权限的根源。见的这里。
I am having trouble creating a .txt file with the code below. I get an exception as follows:
Unhandled exception: System.unauthorizedAccessException: Access to the path 'C:\log.txt' is denied.
I have looked online and done similar things to what is on the api. Below is my code, so you can understand what my train of logic is. What do you think causes this exception? Thanks in advance!
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"); } { } } }
解决方案You're most likely getting this error because a standard user cannot write to the root of a drive without elevated permissions. See here.
这篇关于C#,创建.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!