System.UnauthorizedAccessException的是未处理 [英] System.UnauthorizedAccessException was unhandled
问题描述
我得到一个拒绝访问异常。我如何才能解决这个问题。
下面是个例外:
系统。 UnauthorizedAccessException了未处理的HResult = -2147024891
消息=访问路径C:\message.txt'。被拒绝,结果
来源= mscorlib程序
块引用>
下面是代码:
公共静态无效将writeToFile(字符串s)
{
FS =新的FileStream(C:\\message.txt,
FileMode.Append,FileAccess.Write);
SW =新的StreamWriter(FS);
sw.WriteLine(S);
sw.Flush();
sw.Close();
fs.Close();
}
编辑:这工作,如果我跑vs2012作为管理员,但有什么办法?或者一个理由做它作为普通用户
和这个作品:
公共静态无效将writeToFile(字符串s)
{
FS =新的FileStream(@C:\Users\KristjanBEstur\Documents\message.txt,
的FileMode .Append,FileAccess.Write);
SW =新的StreamWriter(FS);
sw.WriteLine(S);
sw.Flush();
sw.Close();
fs.Close();
File.Delete(@C:\Users\KristjanBEstur\Documents\message.txt);
}
解决方案您可能需要运行在管理员模式下的项目,如果你想访问根目录
您可以通过添加这对应用程序清单
这样做< requestedExecutionLevel水平=requireAdministratoruiAccess =真/>
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=mscorlibHere 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"); }
解决方案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"/>
这篇关于System.UnauthorizedAccessException的是未处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!