-1

Im trying to make file named hello.txt and code show error that I dont have access to the path.

So how can I make file?

Code:

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

Error:

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
  • They way I am reading this is you tried A and got a descriptive error message about why A failed. I am not reading a question, just a statement of events. What is your question. – Igor Aug 06 '21 at 14:40
  • 2
    Well... do you have write access to the folder? Can you create a file there using (for example) the File Explorer? – Boise Aug 06 '21 at 14:42
  • 1
    `So how can I make file?` ← Based on the Exception and message you received you need to grant access rights to the location where you are creating it. – Igor Aug 06 '21 at 14:44
  • Does this answer your question? [Access to the path denied error in C#](https://stackoverflow.com/questions/7706467/access-to-the-path-denied-error-in-c-sharp) – Trevor Aug 06 '21 at 14:50
  • @zaggler I already tried that, not helping. But thanks – user16580310 Aug 06 '21 at 14:52
  • @user16580310 you've tried all solutions (5) in that duplicate and nothing works? `I already tried that`, we need more details in order to help you. – Trevor Aug 06 '21 at 15:06
  • Do directory path and final folder exist? Does the file already exist? Do you have rights and permission to create and write to the folder? What does https://lockhunter.com tell you about the folder and the file? –  Aug 06 '21 at 15:13
  • @OlivierRogier Folder exists and file not cuz Im trying to create it – user16580310 Aug 06 '21 at 15:15
  • @ChrisCatignani Already tried. – user16580310 Aug 06 '21 at 15:15

1 Answers1

-2

If you are using Visual Studio try launching Visual Studio Application as Administrator.