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



我正在尝试编写一个windows窗体应用程序,该应用程序将日志写入中的.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只是.txt文件的名称,我得到了一个例外:

System.UnauthorizedAccessException
HResult=0x80070005
Message=Access to the path 'C:Usersmilos_qhhenDocumentsDNDicelogslog_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:MilosDNDCharacter SheetCharacter SheetLogger.cs:line 35
at Character_Sheet.MainForm.MainForm_Load(Object sender, EventArgs e) in D:MilosDNDCharacter SheetCharacter SheetMainForm.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##

最新更新