簡體   English   中英

C# 訪問路徑被拒絕在 Documents 中創建文件的異常

[英]C# Access to the path is denied exception creating a file in Documents

我正在嘗試編寫一個 windows forms 應用程序,它將日志寫入 a.txt 文件:

Documents/subfolder/name.txt

我可以使用創建子文件夾目錄

        string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        string dirPath = Path.Combine(documentsPath, appFolderName, logFolderSubpath);

        if (!Directory.Exists(dirPath))
        {
            Directory.CreateDirectory(dirPath);
        }

        string fileName = "log" + DateTime.Now.ToString("_yyyy-MM-dd_hh-mm-ss") + ".txt";

        string path = Path.Combine(dirPath, fileName);

但是當我嘗試創建一個 StreamWriter 時:

StreamWriter writer = new StreamWriter(Path.Combine(path, filename));

其中 filename 只是 a.txt 文件的名稱,我得到了異常:

System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Access to the path 'C:\Users\milos_qhhen\Documents\DNDice\logs\log_2021-04-30_10-30-33.txt' is denied.
  Source=mscorlib
  StackTrace:
   at 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)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.StreamWriter.CreateFile(String path, Boolean append, Boolean checkHost)
   at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost)
   at System.IO.StreamWriter..ctor(String path)
   at Character_Sheet.Logger..ctor() in D:\Milos\DND\Character Sheet\Character Sheet\Logger.cs:line 35
   at Character_Sheet.MainForm.MainForm_Load(Object sender, EventArgs e) in D:\Milos\DND\Character Sheet\Character Sheet\MainForm.cs:line 57
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

  This exception was originally thrown at this call stack:
    [External Code]
    Character_Sheet.Logger.Logger() in Logger.cs
    Character_Sheet.MainForm.MainForm_Load(object, System.EventArgs) in MainForm.cs
    [External Code]

我試圖讓我的應用程序將日志文件寫入 Documents 目錄,因為這是一個可供多人使用的程序,我想要一個可以寫入日志的固定位置。 所以我需要我的程序能夠創建目錄,在其中創建文件,然后寫入該文件。 我不希望在 .exe 所在的同一目錄中執行此操作。

也許您使用的用戶對該文件夾沒有寫權限。 嘗試為您的用戶添加寫權限或簡單地以管理員身份運行 VS。 它應該工作

我在 Linux 機器上運行 .net 控制台應用程序時遇到了這個問題。 問題是用戶沒有對該文件夾的執行權限

這個命令為我解決了

sudo chmod -R a+rwx ##folder path##

暫無
暫無

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

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