0

I am trying to create a text file in C: using code (shown in picture) It says access is denied. Please help.

enter image description here

Thanks

Furqan Sehgal
  • 4,917
  • 33
  • 108
  • 167

3 Answers3

5

The root of the C: drive is protected (On Windows Vista, 7, etc), so users cannot write to it unless the process is elevated.

Try right clicking on your compiled exe and then Run As Admin to test this theory.

The solution is to save your file somewhere else. This may help- Lemme Tell Ya Where to Stick It and Where Should I Store my Data and Configuration Files

Or you may want to create a temp file: How can I create a temp file with a specific extension with .NET? - you should have the relevant permissions to save this file but it cannot be guaranteed.

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143
0

The user, who executes the program has no rights to create a file directly into the c:\ drive.

MjeOsX
  • 375
  • 4
  • 12
  • 1
    @FurqanSehgal: If you could do that, then there would be no point in having a security system at all. – Christian Hayter Jun 12 '13 at 08:27
  • You have to use another path or execute the program as Administrator. But you should handle the Exception for example with try catch ... http://msdn.microsoft.com/de-de/library/0yd65esw(v=vs.80).aspx – MjeOsX Jun 12 '13 at 08:29
-1

vsFilePath should be something like c:\log.txt not c:

Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89