Greetings

I add the console program to autoload in this way

if ( this.autorun != 0 ) { Microsoft.Win32.RegistryKey Key = Microsoft .Win32 .Registry .CurrentUser .OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", true); Key.SetValue("имя_программы", Environment.CurrentDirectory + "\\имя_программы.exe"); Key.Close(); } else { Microsoft.Win32.RegistryKey key = Microsoft .Win32 .Registry .CurrentUser .OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\", true); key.DeleteValue("имя_программы", false); key.Close(); } 

On Win7, it works correctly - after booting the system, the program starts and does its job.

The problem occurs on Win Vista - after the system boots, the program starts, but after that it throws an exception. The logs are empty, then the suspicions crept in, made a conclusion to the console and the suspicions were confirmed - the crash crashes when you first try to write to the file.

 System.UnauthorizedAccessException: Отказано в доступе по пути "C:\Windows\system32\error.log". 

error.log is the log file itself. For some reason, trying to write in C:\Windows\system32\ . At first I thought about relative paths, but when specifying absolute paths, it is absolutely the same. When you manually start everything is normal.

It is not clear where to dig.


@Pavel Mayorov

 StreamWriter f = new StreamWriter( Environment.CurrentDirectory + "\\" + "error.log", true ); f.WriteLine( str ); f.Close(); 
  • one
    Why did you describe the autorun mechanism in such detail? Write better as you open the log file. - Pavel Mayorov 5:53 pm
  • @Pavel Mayorov: added. - froxxendsg

1 answer 1

Environment.CurrentDirectory + "/" + "error.log" - do you call this the "absolute path" ?! Obviously, the current default directory is C:\Windows\system32\ . That log file is there.

Where better to place the log file? For applications that are placed in Program Files it is best to place it somewhere in an unmovable user profile. The path to it can be obtained as Enviroment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) .

If the application is portable (that is, it is intended to run from a write access directory), then AppDomain.CurrentDomain.BaseDirectory or Assembly.GetEntryAssembly().Location is a good starting point.

  • Hmm, strangely, for some reason I thought that Environment.CurrentDirectory always returns the current directory with the executable file. - froxxendsg
  • @froxxendsg: Each process has a current directory. If a process is started from the directory where it is located (as it most often happens), and it does not change its current directory (it also happens quite often), then the current directory is the program directory. - VladD
  • @froxxendsg: In any case, writing to System32 or Program Files is a very bad idea. Thank God, starting with Vista, OS doesn’t allow anyone so easily. - VladD
  • @VladD: naturally I didn’t intend to write to System32 or Program Files - froxxendsg
  • @VladD: Executor is on another disk and writes all the logs to its directory. After autoload, for some reason Environment.CurrentDirectory contains not the path to the directory with the executable, but C:\Windows\system32\ . By the way, on Win7, the relative paths worked as they should - they wrote to the directory with the executable. - froxxendsg