System.UnauthorizedAccessException was unhandled

2019-02-27 20:08发布

I am getting a access denied exception. How can I fix this?

Here is the exception:

System.UnauthorizedAccessException was unhandled HResult=-2147024891 Message=Access to the path 'c:\message.txt' is denied.
Source=mscorlib

Here is the code:

    public static void WriteToFile(string s)
    {
        fs = new FileStream("c:\\message.txt",
        FileMode.Append, FileAccess.Write);
        sw = new StreamWriter(fs);
        sw.WriteLine(s);
        sw.Flush();
        sw.Close();
        fs.Close();
    }

EDIT: It works if I run vs2012 as administrator, but is there a way or a reason to do it as normal user?

And this works:

    public static void WriteToFile(string s)
    {
        fs = new FileStream(@"C:\Users\KristjanBEstur\Documents\message.txt",
        FileMode.Append, FileAccess.Write);
        sw = new StreamWriter(fs);
        sw.WriteLine(s);
        sw.Flush();
        sw.Close();
        fs.Close();
        File.Delete(@"C:\Users\KristjanBEstur\Documents\message.txt");
    }

5条回答
何必那么认真
2楼-- · 2019-02-27 20:27

Double click on app.manifest file and if app.manifest not present Right click on your project, add -> New Item -> Application Manifest File then replace this line

<requestedExecutionLevel level="asInvoker" uiAccess="false" />

with this

<requestedExecutionLevel level="requireAdministrator" uiAccess="true" />

It will run your application with administrator privileges.

查看更多
beautiful°
3楼-- · 2019-02-27 20:31

You need to set the FileAtrributes property of the File to Normal before accessing the file.

Try This:

public static void WriteToFile(string s)
    {
        String path=@"c:\message.txt";
        if(File.Exists(path))
        {
        File.SetAttributes(path, FileAttributes.Normal);
        fs = new FileStream(path,
        FileMode.Append, FileAccess.Write);
        sw = new StreamWriter(fs);
        sw.WriteLine(s);
        sw.Flush();
        sw.Close();
        fs.Close();
        }
        else
        {
        MessageBox.Show("File not Found!");
        }
    }
查看更多
ら.Afraid
4楼-- · 2019-02-27 20:36

Just to add to Mujahed's answer (I can't comment yet), you don't need uiAccess="true" if you just wanted to run with administrator privileges. Setting this option to true could cause you some headache (e.g. you might not be able to run your project in debug mode.)

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
查看更多
Explosion°爆炸
5楼-- · 2019-02-27 20:37

I'm not sure exactly what all might throw an UnauthorizedAccessException.

One idea though... you're trying to create a file directly in the c: drive, which is sometimes disallowed by system policy. For the sake of troubleshooting, try creating the file somewhere else that you'd have access to, like My Documents.

查看更多
冷血范
6楼-- · 2019-02-27 20:41

You may need to run your project in administrator mode if you want access to the root directory

You can do this by adding this to the app manifest

<requestedExecutionLevel level="requireAdministrator" uiAccess="true"/>
查看更多
登录 后发表回答