简体   繁体   English

C# 访问路径被拒绝在 Documents 中创建文件的异常

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

I am trying to write a windows forms app that will write logs to a.txt file in:我正在尝试编写一个 windows forms 应用程序,它将日志写入 a.txt 文件:

Documents/subfolder/name.txt

I am able to create a the subfolder directory using我可以使用创建子文件夹目录

        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);

but when I try to create a StreamWriter:但是当我尝试创建一个 StreamWriter 时:

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

where filename is just a name of a.txt file, I get the exception:其中 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]

I am trying to make my app write log files into the Documents directory because this is a program that will be used by multiple people and I want a constant place where I can write the logs to.我试图让我的应用程序将日志文件写入 Documents 目录,因为这是一个可供多人使用的程序,我想要一个可以写入日志的固定位置。 So I need my program to be able to create the directory, to create the file inside it, and then write into that file.所以我需要我的程序能够创建目录,在其中创建文件,然后写入该文件。 I would prefer not to do this in the same directory where the.exe is.我不希望在 .exe 所在的同一目录中执行此操作。

Maybe the user you are using doesn't have write permission on that folder.也许您使用的用户对该文件夹没有写权限。 Try to add write permission for you user or simply run the VS as administrator.尝试为您的用户添加写权限或简单地以管理员身份运行 VS。 It should work它应该工作

I had this issue when running a .net console app on a Linux machine.我在 Linux 机器上运行 .net 控制台应用程序时遇到了这个问题。 the issues was that the user didn't have execute permissions to the folder问题是用户没有对该文件夹的执行权限

this command solved it for me这个命令为我解决了

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM