简体   繁体   English

File.CreateText - 拒绝访问

[英]File.CreateText - Access Denied

I recently spent a while trying to figure out what permissions were needed to get this statement to work:我最近花了一段时间试图弄清楚让这个语句起作用需要什么权限:

if (!File.Exists(path)) 
{
    // Create a file to write to. 
    using (StreamWriter sw = File.CreateText(path)) 
    {
        sw.WriteLine("Hello");
        sw.WriteLine("And");
        sw.WriteLine("Welcome");
    }   
}

I eventually have "Special Permissions" to "Everyone" which should have solved the problem.我最终拥有“每个人”的“特殊权限”,这应该可以解决问题。 Sadly it didn't, it may actually be a bug?可悲的是它没有,它实际上可能是一个错误?

The following works:以下工作:

if (!File.Exists(_logFilePath))
{
    using (var fs = File.Create(_logFilePath))
    {
        using (var sw = new StreamWriter(fs))
        {
             sw.WriteLine("Hello");
             sw.WriteLine("And");
             sw.WriteLine("Welcome");
        }
    }
}

Theoretically identical (I think), but I couldn't get the first to work.理论上相同(我认为),但我无法让第一个工作。

I think the path variable is wrong.我认为路径变量是错误的。 Make sure you've also entered the file name in the path.确保您还在路径中输入了文件名

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM