2

I am having trouble creating a .txt file with the code below. I get an exception as follows:

Unhandled exception: System.unauthorizedAccessException: Access to the path 'C:\log.txt' is denied.

I have looked online and done similar things to what is on the api. Below is my code, so you can understand what my train of logic is. What do you think causes this exception? Thanks in advance!

static StreamWriter swt;
static string logFile = @"C:\log.txt";
static FileStream fs;
static void Main(string[] args)
{
    Console.Out.WriteLine(swt);
    string root =  args[0];                       
     if (!File.Exists(logFile))
     {
         try
         {
             fs = File.Create(logFile);
         }
         catch (Exception ex)
         {
             swt.WriteLine("EXCEPTION HAS BEEN THROWN:\n " + ex + "\n");
         }
         {

         }
     }
 }
Veer
  • 1,575
  • 3
  • 16
  • 40
haysam
  • 401
  • 5
  • 11
  • 16
  • 9
    My guess is that you have insufficient privileges to write directly to the `C:` root. Try running your program as Administrator or write to an accessible directory (like My Documents) – Chris Sinclair Jun 03 '13 at 13:03
  • Let me try that, thanks – haysam Jun 03 '13 at 13:03
  • Your program running in administrative mode? – fhnaseer Jun 03 '13 at 13:04
  • To complete @ChrisSinclair comment, standard windows permissions would allow you to create a directory under c:, where you'll have the permission to write. Simply precede your code by the creation of a folder. – Steve B Jun 03 '13 at 13:04
  • Did you try creating the file in another directory? A normal user without explicit administrative rights may not have the authorization to create a file directly under the root of drive C: ?! **edit**: Obviously I am writing too slow as the comments above were written right in the time where I was writing my comment. ;-) – Felix Bayer Jun 03 '13 at 13:06
  • @haysam See my answer, I've same problem and found working solution by this. – Never Quit Jun 03 '13 at 13:29

3 Answers3

9

You're most likely getting this error because a standard user cannot write to the root of a drive without elevated permissions. See here.

Community
  • 1
  • 1
Ani
  • 10,826
  • 3
  • 27
  • 46
2

Detect the folder permissions. It has to has write permission on the logged user.

kostas ch.
  • 1,960
  • 1
  • 17
  • 30
2

Yes, it is permission error. You don't have enough rights to write file in C: drive. To write in these types of folder/drive you need admin permission.

You can give your application admin rights. Simple way is enforce your application to start in admin account/rights only. To achieve this

Solution Explorer -> your project -> Add new item (right click) -> Application Manifest File.

In this file change requestedExecutionLevel to

<requestedExecutionLevel level="highestAvailable" uiAccess="false" />

These enforce your application with admin rights only. On Windows 8/7/Vista it will display UAC (User Access Control) dialog box when you start the application.

Hope this will help you....

Never Quit
  • 2,072
  • 1
  • 21
  • 44