Search code examples
c#access-denied

Access to the path 'c:\\log.txt' is denied in File.Create


I am trying to create a log file in my program:

if (!File.Exists("c://log.txt"))
{
    File.Create("c://log.txt");
}

I have received this error:

Access to the path 'c:\log.txt' is denied

How can i get access to c ?

Thanks.


Solution

  • I think you have to force .NET to run this programm as administrator:

    How do I force my .NET application to run as administrator?

    But you should write your log file any where else then "C:\", you can use a temp directory like this http://msdn.microsoft.com/de-de/library/system.io.path.gettemppath(v=vs.110).aspx or you use something like "C:\temp"

    If you like to write your log file into the directory of the executing assembly use this to get the directory:

    System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)