【发布时间】:2014-02-28 18:07:20
【问题描述】:
早上,
我正在尝试在桌面上的文件夹中创建一些文本文件,但收到一条消息,提示“UnauthorizedAccessException 未处理”错误,拒绝访问路径“C:\MSGTEST”。
我不明白为什么我会收到此消息:
- 我拥有此目录的完全访问权限。
- UAC 已关闭。
- 我是本地管理员。
谁能告诉我为什么会这样?
以下行的程序炸弹:
using (FileStream MSGFiles = new FileStream(MSGRootPath, FileMode.OpenOrCreate, FileAccess.Write))
我将文本文件写入目录的代码:
using (FileStream MSGFiles = new FileStream(MSGRootPath, FileMode.OpenOrCreate, FileAccess.Write))
using (StreamWriter sw = new StreamWriter(MSGFiles))
{
while (readerObj.Read())
{
SqlMsg = readerObj["Msg"].ToString();
SqlRef = readerObj["Reference"].ToString();
SqlType = readerObj["Type"].ToString();
//SqlTime = readerObj["Time"].ToString();
//SqlTime = SqlTime.Replace(":", "-");
//SqlTime = SqlTime.Replace("/", "-");
sw.WriteLine(SqlRef + "_" + SqlType + ".txt");
异常错误:
System.UnauthorizedAccessException 未处理 H结果=-2147024891 消息=拒绝访问路径“C:\MSGTEST”。 源=mscorlib 堆栈跟踪: 在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径) 在 System.IO.FileStream.Init(字符串路径、FileMode 模式、FileAccess 访问、Int32 权限、Boolean useRights、FileShare 共享、Int32 bufferSize、FileOptions 选项、SECURITY_ATTRIBUTES secAttrs、String msgPath、Boolean bFromProxy、Boolean useLongPath、Boolean checkHost) 在 System.IO.FileStream..ctor(字符串路径,FileMode 模式,FileAccess 访问) 在 SwiftMSGDownload.Form1.subGetMSGs() 在 c:\Users\nasswee\Documents\Visual Studio 2012\Projects\SwiftMSGDownload\SwiftMSGDownload\Form1.cs:line 47 在 SwiftMSGDownload.Form1.btnStart_Click(Object sender, EventArgs e) 在 c:\Users\nasswee\Documents\Visual Studio 2012\Projects\SwiftMSGDownload\SwiftMSGDownload\Form1.cs:line 99 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs 事件) 在 System.Windows.Forms.Control.WmMouseUp(消息和 m,MouseButtons 按钮,Int32 点击) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.ButtonBase.WndProc(消息和 m) 在 System.Windows.Forms.Button.WndProc(消息和 m) 在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(消息& m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(味精和味精) 在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.Run(窗体 mainForm) 在 c:\Users\nasswee\Documents\Visual Studio 2012\Projects\SwiftMSGDownload\SwiftMSGDownload\Program.cs:line 18 中的 SwiftMSGDownload.Program.Main() 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:
有什么想法吗?
【问题讨论】:
-
不要使用根文件夹来对抗系统。使用
Environment.SpecialFolder名称之一。 -
以管理员身份运行是否有效?
-
Enviornment.SpecialFolder 不是一个选项,因为最终目标将是服务器共享。但是当我指向共享时,我遇到了同样的错误。
-
以管理员身份运行 VS 仍然会出现同样的错误。
标签: c# filestream access-denied unauthorizedaccessexcepti