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

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

Patrick Steele [MVP]

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?
 
G

Guest

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
 
P

Patrick Steele [MVP]

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

Guest

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
 
P

Patrick Steele [MVP]

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

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top