Problem Writing to a File - Urgent! Please Help!!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

In a winform application (in C#) I am trying to write to a file with the following:

String filepath = @"C:\Documents and Settings\MyAccount\MyDirectory";
FileStream fsCurrentFile = new FileStream(filepath,FileMode.Append);
StreamWriter sw = new StreamWriter(fsCurrentFile);

But I get the following error message:

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access to the path "C:\Documents and Settings\MyAccount\MyDirectory" is denied.

I don't understand why I get that because I debug on an Administrator acount.
It is written that it may be because the Directory is read-only.
I checked the Directory, it was read-only; I removed the read-only but it comes back
by some magic I don't know about. All my directories seemed to be Read-Only, and I can't change that (when I
do the change, I comes back afterward!).

Any idea what I could do??

Thanks a million!

Jenny

PS: I just installed VS 2003, maybe it is related to that...
 
In a winform application (in C#) I am trying to write to a file with the following:

String filepath = @"C:\Documents and Settings\MyAccount\MyDirectory";
FileStream fsCurrentFile = new FileStream(filepath,FileMode.Append);
StreamWriter sw = new StreamWriter(fsCurrentFile);

But I get the following error message:

An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access to the path "C:\Documents and Settings\MyAccount\MyDirectory" is denied.

Are you running the application off a network drive?
 
No I don't run the application from a Network drive
Everything is on my machine

I really don't understand what is going on..
I can't remove the Read-Only attributes on my directories

Jenn
 
No I don't run the application from a Network drive.
Everything is on my machine.

I really don't understand what is going on...
I can't remove the Read-Only attributes on my directories.

Are you sure you've got the right path? You can get the "My Documents"
directory for the current user by using:

Environment.GetFolderPath(Environment.SpecialFolder.Personal)
 
I am creating a new file therefore the exact directory is not important
I did several tests on several directories, and none of them worked
I get this "Access Denied" error on all the directories of my system,
even if I create new ones..

thanks for all your help!!

Jenny
 
I am creating a new file therefore the exact directory is not important.
I did several tests on several directories, and none of them worked.
I get this "Access Denied" error on all the directories of my system,
even if I create new ones...

Very odd... The majority of times I see this error when the application
is executed from a network share or a drive mapped to a network share
(i.e. not coming from a local hard drive). If it doesn't come from a
local hard drive, it has different security policy.
 
We just had the exact same problem. Our problem is that we forgot to append the filename to the directory. Doh! So don't forget to do the following:

String filename = @"test.txt";
String filepath = @"C:\Documents and Settings\MyAccount\MyDirectory";
filepath = path.Combine(filepath, filename);
FileStream fsCurrentFile = new FileStream(filepath,FileMode.Append);
StreamWriter sw = new StreamWriter(fsCurrentFile);

Good luck
 
Back
Top