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();