Access to the path denied error in C#
Asked Answered
C

8

69

I have read a similar post, but i just cant figure out the problem.

I have changed the windows permissions and changed routes.

When i try to save a file it throws me the exception:

Access to the path **** denied.

string route="D:\\";
FileStream fs = new FileStream(route, FileMode.Create); <--here is the problem
        StreamWriter write = new StreamWriter(fs);
        patient person = new patient();
        patient.name = textBox1.Text;
        patient.name2 = textBox2.Text;
Caldeira answered 9/10, 2011 at 20:48 Comment(3)
When you say you've changed the windows permissions, explain exactly what you mean. Is D:\ a network share, local drive, external drive? Is it a CD-Rom?Angelangela
D:\ is a partition on my HDD. But i have changed it to C:\ and My Documents. None works.Caldeira
None of the solutions provided here worked for me.Can
S
165

You are trying to create a FileStream object for a directory (folder). Specify a file name (e.g. @"D:\test.txt") and the error will go away.

By the way, I would suggest that you use the StreamWriter constructor that takes an Encoding as its second parameter, because otherwise you might be in for an unpleasant surprise when trying to read the saved file later (using StreamReader).

Scrutator answered 9/10, 2011 at 20:53 Comment(2)
A doubt. How can i read the route of the place where the user wants to save the file so i can save the route in string route?Caldeira
@aerojun: This is actually another question, but I think what you will need (assuming "route" denotes "path") is the SaveFileDialog class as well as the path management methods in the static Path class.Scrutator
G
15

Did you try specifing some file name?

eg:

string route="D:\\somefilename.txt";
Glenn answered 9/10, 2011 at 20:55 Comment(0)
B
12

tl;dr version: Make sure you are not trying to open a file marked in the file system as Read-Only in Read/Write mode.

I have come across this error in my travels trying to read in an XML file. I have found that in some circumstances (detailed below) this error would be generated for a file even though the path and file name are correct.

File details:

  • The path and file name are valid, the file exists
  • Both the service account and the logged in user have Full Control permissions to the file and the full path
  • The file is marked as Read-Only
  • It is running on Windows Server 2008 R2
  • The path to the file was using local drive letters, not UNC path

When trying to read the file programmatically, the following behavior was observed while running the exact same code:

  • When running as the logged in user, the file is read with no error
  • When running as the service account, trying to read the file generates the Access Is Denied error with no details

In order to fix this, I had to change the method call from the default (Opening as RW) to opening the file as RO. Once I made that one change, it stopped throwing an error.

Bonnice answered 2/5, 2014 at 15:24 Comment(2)
This solved my problem: FileStream fs = new FileStream(szFileName, FileMode.Open, FileAccess.Read);Robi
This one worked with @FelipeGavilán addition, the file stream that I had set up did not have the extra peram, I wonder now what will happen on the rest of the CRUD operations.Goulash
R
4

I had this issue for longer than I would like to admit.

I simply just needed to run VS as an administrator, rookie mistake on my part...

Hope this helps someone <3

Rudie answered 5/12, 2019 at 11:50 Comment(0)
C
1

You do not have permissions to access the file. Please be sure whether you can access the file in that drive.

string route= @"E:\Sample.text";
FileStream fs = new FileStream(route, FileMode.Create);

You have to provide the file name to create. Please try this, now you can create.

Cuirbouilli answered 4/6, 2013 at 1:28 Comment(0)
S
1

If your problem persist with all those answers, try to change the file attribute to:

File.SetAttributes(yourfile, FileAttributes.Normal);
Scalawag answered 15/2, 2019 at 14:4 Comment(0)
C
0

TLDR : On my end, it had something to do with AVAST ! => Whitelist your application.


All of a sudden, I also got this UnauthorizedAccessException problem in the windows WPF program I'm writing. None of the solutions worked - except I couldn't figure out how to elevate my application to full privileges (not using VS) while at the same time, being already on the administrator account, I didn't feel the need to dig that deep in permission concerns.

The files are image files (jpg, psd, webp, etc.) I wasn't trying to open/write a directory, it has always been a valid path to a file, and I needed to write to the file, FileAccess.ReadWrite was inevitable. The files (and any of their parent directory) were not readonly (I even checked by code prior calling new FileStream(path, mode, access, share) via FileInfo.IsReadOnly) - so what happenned all of a sudden ???

Thinking about : I had an had drive crash, so I unpacked a backup of my solution code from another drive. In the meantime, I added codes in my application to PInvoke APIs to directly read hard drive sectors physical bytes as well as USB plug/unplug monitoring.

I started to get the Exception when I added those, but even though I temporarly removed the related codes from the application, I still got the UnauthorizedAccessException.

Then I remembered one thing I've done long ago, a painstaking similar issue where I wanted my application to communicate sensible data via Wifi, which was to add the executable among AVAST exceptions, and the assembly directory aswell (My app was already among the authorized apps through firewall)

Just did it for my application in AVAST settings, AND THE EXCEPTION IS GONE !!! Two whole days I'm lurking StackOverflow and the web to get moving on, FINALLY !


Details : I can't pinpoint exactly what AVAST didn't like in my application as the only changes I made :

  1. Retrieved then launched the backup code - it worked like a charm, files (images) opens/write without problems (3 days ago)
  2. Added USB detection (3 days ago - Just tested the code, didn't tried to open an image)
  3. Added PInvoke physical drive direct read (2 days ago - FileStream, and the logic to define where/how to scan the damaged drive - Just tested the code, didn't tried to open an image)
  4. Added image format detection starting from Jpg/Jfif.. 2 days ago, got the exception upon testing the code.
  5. While searching for solutions, added an Image Gallery WPF UserControl to diplay pictures based on their signature and check which files gives the exception : almost all of them (some files opens/write okay - why ???)
  6. Tried everything I've found on SO (since the last 2 days) until I opened AVAST settings and whitelist my application.
  7. ... now I can move on into adding a bunch of file signatures to retrieve as many datas as I could.

If this may help those who like me, aren't failing on the "I'm passing a directory path instead that of a file", yet, have no time to learn exactly why antiviruses think our own code is a malware.

Conspicuous answered 17/4, 2022 at 17:8 Comment(0)
S
-3

Just Using the below worked for me on OSX.

var path = "TempForTest";
Scaler answered 18/5, 2021 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.