访问被拒绝写入文件夹,为什么?

时间:2014-02-04 16:36:55

标签: c# filestream access-denied unauthorizedaccessexcepti

上午,

我正在尝试创建一些文本文件到我桌面上的文件夹但是收到一条消息“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未处理       的HResult = -2147024891       消息=拒绝访问路径“C:\ MSGTEST”。       来源= mscorlib程序       堆栈跟踪:          在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)          at System.IO.FileStream.Init(String path,FileMode mode,FileAccess access,Int32 rights,Boolean useRights,FileShare share,Int32 bufferSize,FileOptions options,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:第47行          在SwiftMSGDownload.Form1.btnStart_Click(Object sender,EventArgs e)中的c:\ Users \ nasswee \ Documents \ Visual Studio 2012 \ Projects \ SwiftMSGDownload \ SwiftMSGDownload \ Form1.cs:第99行          在System.Windows.Forms.Control.OnClick(EventArgs e)          在System.Windows.Forms.Button.OnClick(EventArgs e)          在System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)          在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(Message& m)          在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)          在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)          在System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)          在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32 reason,Int32 pvLoopData)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context)          在System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)          在System.Windows.Forms.Application.Run(Form mainForm)          在SwiftMSGDownload.Program.Main()的c:\ Users \ nasswee \ Documents \ Visual Studio 2012 \ Projects \ SwiftMSGDownload \ SwiftMSGDownload \ Program.cs:第18行          在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)          在System.AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)          在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()          在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)          at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)          at System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean preserveSyncCtx)          在System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)          在System.Threading.ThreadHelper.ThreadStart()       的InnerException:

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

即使您是本地管理员,也可能没有“以管理员身份”运行程序

右键单击Visual studio,如果要以管理员身份调试程序,请选择“以管理员身份运行”。

答案 1 :(得分:0)

这个问题的最快解决方案是允许对每个人的完全权限到此路径“C:\ MSGTEST”(这是一个主要的安全问题)正确的方法是允许persleisions onle IUSR或IIS_IUSERS取决于您的IIS版本

查看以下link

答案 2 :(得分:0)

我不确定MSGRootPath指向的确切位置,但我怀疑它包含错误报告中的值“C:\ MSGTEST”。 如果是这种情况,您将收到此异常,因为Windows不允许您直接写入文件夹,就像您似乎尝试一样。 (因为,打开文件夹是不可能的,我不知道他们为什么决定让它通过一个UnauthorizedAccessException)

在阅读了对其他答案的评论之后,这可能会改为:在while循环中打开文件流,而不是名称MSGRootPath + SqlRef + "_" + SqlType + ".txt"