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

15,100

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)
Share:
15,100
user2153436
Author by

user2153436

Updated on August 21, 2022

Comments

  • user2153436
    user2153436 over 1 year

    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.