473,320 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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...
Nov 15 '05 #1
8 1569
In article <66**********************************@microsoft.co m>,
an*******@discussions.microsoft.com says...
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?

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 15 '05 #2
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
Nov 15 '05 #3
In article <37**********************************@microsoft.co m>,
an*******@discussions.microsoft.com says...
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.SpecialFolde r.Personal)

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 15 '05 #4
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
Nov 15 '05 #5
In article <AF**********************************@microsoft.co m>,
an*******@discussions.microsoft.com says...
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.

--
Patrick Steele
Microsoft .NET MVP
http://weblogs.asp.net/psteele
Nov 15 '05 #6
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
Nov 16 '05 #7
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
Nov 16 '05 #8
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
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Edwinah63 | last post by:
Hi Everyone, All the very best for 2004!! i need urgent help with this problem, the users are about to skin me alive!! we have an access front end with linked to sql server 2k tables. ...
1
by: Jenny | last post by:
Need urgent help for an unsolved problem. In our ASP web application, we creat a Back button and if user click on this button, it execute history.go(-1) to go back to the previous page. All our...
1
by: Rob | last post by:
Hi everyone, I'm having some big JS problems - my function is making my html code vanish, and I need help! Here's the code, try it for yourself. You'll notice that when you click either links...
7
by: zeyais | last post by:
Here is my HTML: <style> ..leftcolumn{float:left;width:300px;border: 1px solid #ccc} ..rtcolumn{float:left;width:600px;border: 1px solid #ccc} </style> <body> <div class="leftcolumn"...
1
by: brabster | last post by:
Hi, this is my first post so please excuse any mistakes! I'd like to write files using an upload script in my website, but I'm having problems. I don't seem to be able to write a file. Ive set up...
1
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
0
by: kingp | last post by:
Hi all I would like to request for a help here.I am working with db2 databases and currently been assigned a job to migrate from oracle to Db2.I have been struggling with a problem and being...
1
by: Domini | last post by:
Hi all, I need urgent help with an xml issue. Let me explain the scenario: My VB.NET app needs to read an xml (e.g. Sales Orders). This xml contains say 10 sales orders. Next the app needs to split...
5
werks
by: werks | last post by:
Hello experts. I have 2 vbp (namely Project1.vbp and Project2.vbp) I Group them into 1 Project, my problem is how can the user choose on what project they want to use using VB6? My initial idea...
1
by: =?Utf-8?B?Y2phbXBz?= | last post by:
I have HL-DT-ST RW/DVD GCC-4240N. It seems to be a problem though with either the cd rom drive or the operating system because whatever CD I put in it does not recognize as a CDR or a CDRW...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.