简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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