Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I am trying to open a standard text file that I have created in a directory within the project solution directory and I get an UnauthorizedAccessException. The application is set to be a full-trust application. I am accessing the file by its full path, including the drive letter), and the exception is being generated in the constructor of a StreamReader. The permissions on the folder are inherited from the root drive and Owner/Creator has full read/write permission.

I know I should read the complete ins-and-outs of .NET security, but this is a quick-and-dirty test application and I am on a very tight deadline. I am hoping I am doing something obvious wrong.

Any ideas very gratefully received.

Best wishes, Patrick

The code is as follows:

private void buttonDrawGraph_Click( object sender, EventArgs e )
{
	char[] splitCharacter = new char[] { ',' };
	try
	{
		using ( StreamReader textFileReader = new StreamReader( @"D:\Dev\Projects\DLP\DLP\data" ) )
		{
			while ( ( string oneLine = textFileReader.ReadLine() ) != null ) 
					{
					}
		}
	}
	catch ( System.IO.FileNotFoundException ) 
	{
		MessageBox.Show( "Could not open file '" + textBoxDataFile.Text + "'" );
	}
}


The line where the StreamReader is created causes the exception.
Posted
Updated 23-Jun-10 3:36am
v4
Comments
Patrick Skelton 23-Jun-10 9:45am    
I have just discovered that having a relative path from the location of the .exe works fine, so it seems that the absolute path is to blame for some reason.

Apologies for not experimenting with all this stuff BEFORE posting, but I'm in a bit of a panic (and therefore guaranteed to make a bollox of everything I do).

It may not be security related.

"An UnauthorizedAccessException exception is thrown when the operating system denies access because of an I/O error or a security error."

Patrick Skelton wrote:
I know I should read the complete ins-and-outs of .NET security, but this is a quick-and-dirty test application and I am on a very tight deadline.


Never enough time to do it right. :rolleyes:

Patrick Skelton wrote:
I am hoping I am doing something obvious wrong.


Since you have provided no code for review it would be difficult to determine that now wouldn't it. However, since you say it isn't working, then yes you have obviously done something wrong.
 
Share this answer
 
Doh - very sorry. Stupid mistake. The code was trying to open a directory and not a file within the directory.

Guess I need to slow down and stop running round like a headless chicken.

Sorry if I have wasted anyone's time.

-Patrick
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900