Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch wpf Application on Windows startup

Tags:

c#

.net

windows

wpf

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);
like image 740
Neal Avatar asked Jan 25 '14 08:01

Neal


1 Answers

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");
like image 120
SmithMart Avatar answered Oct 10 '22 09:10

SmithMart