11

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);
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
Neal
  • 115
  • 1
  • 5

1 Answers1

12

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");
Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
SmithMart
  • 2,731
  • 18
  • 35
  • In your case you can just set the [Environment.CurrentDirectory](http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory(v=vs.110).aspx). – Hamlet Hakobyan Jan 25 '14 at 08:37
  • 2
    Be careful with Environment.CurrentDirectory it returns the working directory, so you could end up in the same boat. – SmithMart Jan 25 '14 at 08:39