I have developed a WPF application, now I have to launch the application on windows startup.
To do this, I have written the below code. I got the solution from this answer.
It is adding the key in registry but not launching the application.
Microsoft.Win32.RegistryKey key = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
string str = Assembly.GetExecutingAssembly().Location;
key.SetValue("Camaleone", str);
When you start an application normally by double clicking, the working directory is normally the path of the exe file. This means if you reference any settings files in your code it can find them.
But, when you add it to the registry to run on startup, the working directory is c:\windows\system32
because it is started by windows itself.
I normally use this:
public static string BaseDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
this means that BaseDir
is now the path to the exe.
Whenever I reference any files, eg a settings file I would use:
string mySettingsFile = Path.Combine(BaseDir, "MySettingsFile.xml");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With