Click here to Skip to main content
15,887,585 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,
I am getting back to doing a project that was put on hold (now it's urgent?). The issues is the program uses a file in the root of the C: drive for logging data.
The code below was used to see if the file is there if not create it. Now I have had to change PC's and Operating Systems as the XP machine this was written on died
Now running on Win7 Pro the code errors at the below points

C#
IniFile ini = new IniFile(INIPath + "\\Widget_emmulation.ini");
           MessageBox.Show("Hello");

           FileName = "C:\\Vapour.txt";

           if (!File.Exists(FileName))
           {
               StreamWriter sw = new StreamWriter(FileName);
        sw.WriteLine("Created By " + txtAdminName.Text + "  " + DateTime.Now + "\n");
               sw.WriteLine();
               sw.Close();

           }
           else
           {
               StreamWriter sw = File.AppendText(FileName);
          sw.WriteLine("Open By " + txtAdminName.Text + " at " + DateTime.Now + "\n");
               sw.WriteLine();
               sw.Close();

           }
Is this due to the code or a security setting. (I can see issues if you have to Admin rights on the machine to save/alter this file!) Why is it firing an UnauthorizedAccessException.....
Glenn
(Hmmm try...catch here we come!)

Further investigation reveals that it is the line
StramWriter sw = new StreamWriter(FileName); so it appears to be the program if it has to creates the file. It does this in both the version run in debug mode and the compiled (.exe) version?
Posted
Updated 5-Nov-12 23:22pm
v4

1 solution

Yes - and no.

Win7 (and Vista before it) will not let you write any file to the root directory of your boot drive without Admin permissions, as a security measure against boot system viruses - XP and earlier did allow it.

So in a way, yes, it is Win7 that is responsible, but in practice it is bad design on your apps part to try and do that in teh first place! :laugh:

I would suggest that you store data in places where you will always have permission: have look here Where should I store my data?[^]
 
Share this answer
 
Comments
glennPattonWork3 6-Nov-12 6:42am    
Not my Practice! Following specs written Years ago!! some software will freak if it's not in the "right" place. More emails to a client that might or might not rage at me ah well it's not Monday!
Glenn
OriginalGriff 6-Nov-12 6:48am    
Sorry about that - but I canna' change the laws of Microsoft Physics! :laugh:
glennPattonWork3 6-Nov-12 6:54am    
Mmmm, User Data looks good!

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