Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have an interesting issue with writing a text file to the C: drive using the command below:
C#
if (!File.Exists(FileName))
{
    StreamWriter sw = new StreamWriter(FileName);
    sw.WriteLine(TextReplyTrim + "\n");
    sw.WriteLine();
    sw.Close();
}
else
{
    StreamWriter sw = File.AppendText(FileName);
    sw.WriteLine(TextReplyTrim + "\n");
    sw.WriteLine();
    sw.Close();
}

This works or at least appears to work on XP I copied it across to the Windows7 machine this morning. It complains about about permissions which my user account on my PC should have? I didn't notice this until I delete the file it was saving to as it got too scrambled it will up date (the Append will work) just not the create?
Glenn (Happy New Year all!)
Posted

C:\ became more carefully protected with Vista, and continues to be so. You can usually get round the problem by running the program with administrator privileges. However, it is recommended that you do not use this location to store your data, but use one of the Special Folders[^].
 
Share this answer
 
v2
Comments
glennPattonWork3 3-Jan-13 8:13am    
Awww Nutz! I forgot that. Dang it, why I use an XP machine at home for an idea that seemed good. Thank You (Face, Palm etc!!!)
fjdiewornncalwe 3-Jan-13 10:50am    
+5.
Assign read /write permission to user on c drive.
 
Share this answer
 
Comments
glennPattonWork3 3-Jan-13 8:13am    
(sorry, please see above!)
fjdiewornncalwe 3-Jan-13 10:50am    
My 1. This is bad practice. There are reasons why permissions are restricted to the root of the drive and this should not be circumvented. The correct solution is Richard's(Solution 1)

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