Launching the application at startup, using the wrong download path

I use the registry key to configure my application to boot to Windows (after a user logs in). My code is:

RegistryKey RegKey = Registry.LocalMachine; RegKey = RegKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); RegKey.SetValue("AppName", "\"" + @"C:\Users\Name\Desktop" + "\""); RegKey.Close(); 

Thus, with this code the application loads at startup, however the working directory

C: \ Windows \ System32

Does anyone know why?

This does not work for me, because this program requires several files in the same directory as for work. If the program is downloaded to my selected directory ("C: \ Users \ Name \ Desktop"), then the problem will not exist.

Anyone have any suggestions for this?

+4
source share
2 answers

Directory.SetCurrentDirectory() can be used to set the working directory at application startup. The exe path can be obtained using Application.ExecutablePath .

Combine them:

 var fi = new FileInfo(Application.ExecutablePath); Directory.SetCurrentDirectory(fi.DirectoryName); 
+3
source

I figured out a cheap trick on how to do this. When the application starts, read the registry again to get the application launch path (the one you intended). For example: Appl1 has a startup path of "C: \ Users \ Name \ Desktop \ App1.exe".

Once you read the registry for this path, set it as the current directory. Something like that:

  RegistryKey RegKey = Registry.LocalMachine; RegKey = RegKey.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false); string Path = RegKey.GetValue("App1.exe").ToString(); Path = Path.Replace(@"\App1.exe"", ""); // Now it a valid directory. Directory.SetCurrentDirectory(Path); 

It worked for me, but if anyone has a better method, I would love to hear them.

-1
source

Source: https://habr.com/ru/post/1444035/


All Articles