簡體   English   中英

訪問被拒絕寫入文件夾,為什么?

[英]Access denied to write to folder, Why?

早上,

我試圖在桌面上的文件夾中創建一些文本文件,但收到一條消息,提示“未處理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”。 Source = mscorlib StackTrace:位於System.IO .__ Error.WinIOError(Int32 errorCode,字符串可能是FullPath)位於System.IO.FileStream.Init(字符串路徑,FileMode模式,FileAccess訪問,Int32權限,布爾值useRights,FileShare共享,Int32 bufferSize, FileOptions選項,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布爾bFromProxy,布爾useLongPath,布爾checkHost在System.IO.FileStream..ctor(字符串路徑,FileMode模式,FileAccess訪問),在c:\\ Users中的SwiftMSGDownload.Form1.subGetMSGs() \\ nasswee \\ Documents \\ Visual Studio 2012 \\ Projects \\ SwiftMSGDownload \\ SwiftMSGDownload \\ Form1.cs:SwiftMSGDownload.Form1.btnStart_Click(Object sender,EventArgs e)中的第47行,位於c:\\ Users \\ nasswee \\ Documents \\ Visual Studio 2012 \\ Projects \\ SwiftMSGDownload \\ SwiftMSGDownload \\ Form1.cs: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(Message&m,Mous eButtons按鈕,Int32單擊),位於System.Windows.Forms.ButtonBase.WndProc(Message&m),位於System.Windows.Forms.Button.WndProc(Message&m),位於System.Windows.Forms.Button.WndProc(Message&m) System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)處的.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m),System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam, System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG&msg)位於System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID,Int32原因,Int32 pvLoopData) System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32原因,ApplicationContext上下文)的System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32原因,ApplicationContext上下文) )在SwiftMSGDownload.Prog ram.Main()在c:\\ Users \\ nasswee \\ Documents \\ Visual Studio 2012 \\ Projects \\ SwiftMSGDownload \\ SwiftMSGDownload \\ Program.cs:System.AppDomain._nExecuteAssembly的第18行(System.AppDomain的RuntimeAssembly程序集,String [] args) Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()處的System.Threading.ThreadHelper.ThreadStart_Context(對象狀態)處的System.Threading.ExecutionContext.RunInternal(ExecutionContext執行上下文,處為.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args) ContextCallback回調,對象狀態,系統System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback回調,對象狀態,布爾值保持SyncCtx)位於System.Threading.ExecutionContext.Run(ExecutionContext執行上下文,ContextCallback回調,對象狀態) .Threading.ThreadHelper.ThreadStart()InnerException:

有任何想法嗎?

即使您是本地管理員,也可能不會以“管理員”身份運行程序

如果要以管理員身份調試程序,請右鍵單擊Visual Studio,然后選擇“以管理員身份運行”。

解決此問題的最快方法是允許所有人對此路徑“ C:\\ MSGTEST”具有完全權限 (這是一個主要的安全問題),正確的方法是根據您的IIS版本允許僅對IUSR或IIS_IUSERS進行滲透

看一下以下鏈接

我不確定MSGRootPath指向的確切位置,但是我懷疑它包含錯誤報告中的值“ C:\\ MSGTEST”。 在這種情況下,您將收到此異常,因為Windows不允許您像嘗試嘗試的那樣直接寫入文件夾。 (因為打開文件夾進行寫入是不可能的,我不知道為什么他們決定讓它拋出UnauthorizedAccessException)

在閱讀對另一個答案的評論之后,這可能會起作用:在while循環內打開文件流,而不是名稱MSGRootPath + SqlRef + "_" + SqlType + ".txt"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM