【问题标题】:Access denied to write to folder, Why?访问被拒绝写入文件夹,为什么?
【发布时间】: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


【解决方案1】:

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

在阅读您的 cmets 关于另一个答案后,这可能会起作用:在 while 循环中打开文件流,而不是使用名称 MSGRootPath + SqlRef + "_" + SqlType + ".txt"

【讨论】:

    【解决方案2】:

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

    看看下面的link

    【讨论】:

    • 尝试向该文件夹中的每个人提供完全权限,但仍然出现相同的错误。
    • 顺便说一句,我没有为这个程序使用 IIS。
    • 是否有其他进程正在获取该文件夹的所有权,从而使您无法访问它?
    • 不,我不这么认为。有趣的是,我尝试了一种不同的方法:使用 (FileStream MSGFiles = new FileStream(@"\\123.456.78.9\msgtest\test.txt", FileMode.OpenOrCreate)) 而不是我在上面发布的行。这在文件夹中创建了一个 test.txt 文件,并在文件中写入了我的文件名。但我希望它在目录中而不是在文本文件中创建文件名。明白了吗?
    • 工作正常:)
    【解决方案3】:

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

    如果您想以管理员身份调试程序,请右键单击 Visual Studio,然后选择“以管理员身份运行”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 2018-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多