简体   繁体   English

System.UnauthorizedAccessException未处理

[英]System.UnauthorizedAccessException was unhandled

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. System.UnauthorizedAccessException未处理HResult = -2147024891 Message =拒绝访问路径'c:\\ message.txt'。
Source=mscorlib 来源= 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? 编辑:如果我以管理员身份运行vs2012,它是否有效,但有没有办法或理由以普通用户身份执行?

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");
    }

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 双击app.manifest文件,如果app.manifest不存在右键单击您的项目,添加 - >新建项目 - >应用程序清单文件然后替换此行

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

with this 有了这个

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

It will run your application with administrator privileges. 它将以管理员权限运行您的应用程序。

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"/>

I'm not sure exactly what all might throw an UnauthorizedAccessException . 我不确定所有人可能会抛出UnauthorizedAccessException

One idea though... you're trying to create a file directly in the c: drive, which is sometimes disallowed by system policy. 但有一个想法......你正试图直接在c:驱动器中创建一个文件,有时系统策略不允许这样做。 For the sake of troubleshooting, try creating the file somewhere else that you'd have access to, like My Documents . 为了排除故障,请尝试在您可以访问的其他位置创建文件,例如“ My Documents

You need to set the FileAtrributes property of the File to Normal before accessing the file. 您需要在访问文件之前将FileFileAtrributes属性设置为Normal

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!");
        }
    }

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. 只是为了添加Mujahed的答案(我还不能发表评论),如果您只是想以管理员权限运行,则不需要uiAccess="true" Setting this option to true could cause you some headache (eg you might not be able to run your project in debug mode.) 将此选项设置为true可能会让您感到头痛(例如,您可能无法在调试模式下运行项目。)

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

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

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