Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access to the path denied error in C#

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; 
like image 720
aerojun Avatar asked Oct 09 '11 20:10

aerojun


People also ask

What does it mean access to path denied?

You may see “Access to the path is denied” error if the application is not able access to a path that is trying to read or write. This will show up as 401 Unauthorized error in IIS logs.

How do I fix access denied in Visual Studio?

Right-click Visual Studio <VersionNumber>, select Change, and then select Repair to initiate the repair process.

What is UnauthorizedAccessException C#?

An UnauthorizedAccessException exception is thrown when the operating system denies access because of an I/O error or a security error. If you are attempting to access a file or registry key, make sure it is not read-only.

How do I fix command prompt access denied?

Start a Command Prompt as Administrator by right-clicking on the "Command Prompt" icon in the Windows Start Menu and choose "Run as administrator". Click Continue if you are presented with a confirmation popup message box. In the new command prompt, enter "net user administrator /active:yes".


1 Answers

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).

like image 64
Alan Avatar answered Sep 22 '22 06:09

Alan