c# - C# 访问路径被拒绝异常在文档中创建文件
问题描述
我正在尝试编写一个 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:\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。它应该工作
推荐阅读
- angular5 - 如何在 ngFor 数组结果之间添加空格
- redirect - Netlify 重定向不起作用
- c++ - 不能在友元函数中使用重载运算符
- python - 如何在我的 Seaborn FactorPlot/FacetGrid 中增加图例的字体大小?
- css - css 绝对没有涵盖所有内容
- spring-boot - 只能为 HMAC 签名指定 Base64 编码的密钥字节
- postgresql - Postgres 9.6 - 包含 jsons 数组的复杂查询 jsonb 字段
- c++ - 容器在使用前被销毁/修改时的 C++ 警告(通过引用元素或迭代器使用)
- react-native - 如何在本机反应中将字符串转换为base64?
- python - 使用 numpy 在 3D 网格上广播点积