【问题标题】:c# file creation access deniedc# 文件创建访问被拒绝
【发布时间】:2021-08-06 14:39:29
【问题描述】:

我正在尝试制作名为 hello.txt 的文件,并且代码显示我无权访问该路径的错误。

那么我该如何制作文件呢?

代码:

        static void create_file(string path)
        {
            FileStream fs = File.Create(path);
        }
        static void Main(string[] args)
        {
            create_file("hello.txt");
            
        }

错误:

System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Access to the path 'C:\Users\user\Desktop\Bored\Bored\bin\Debug\netcoreapp3.1\hello.txt' is denied.
  Source=System.Private.CoreLib
  StackTrace:
   at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)
   at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   at System.IO.File.Create(String path)
   at Bored.Program.create_file(String path) in C:\Users\user\Desktop\Bored\Bored\Program.cs:line 56
   at Bored.Program.Main(String[] args) in C:\Users\user\Desktop\Bored\Bored\Program.cs:line 61

【问题讨论】:

  • 我读到的方式是您尝试了 A 并收到关于 A 失败原因的描述性错误消息。我不是在阅读问题,只是对事件的陈述。你有什么问题。
  • 嗯...您有对该文件夹的写入权限吗?您可以使用(例如)文件资源管理器在那里创建文件吗?
  • So how can I make file? ← 根据您收到的异常和消息,您需要授予对创建它的位置的访问权限。
  • 这能回答你的问题吗? Access to the path denied error in C#
  • @zaggler 我已经尝试过了,没有帮助。不过谢谢

标签: c# createfile


【解决方案1】:

如果您使用的是 Visual Studio,请尝试以管理员身份启动 Visual Studio 应用程序。

【讨论】:

    猜你喜欢
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多