0

and thank you for taking your time to help me.

I have an app that I'm adding to startup in registry with the code:

RegistryKey setRunAtStartup = Registry.LocalMachine.OpenSubKey  ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
        setRunAtStartup.SetValue("mls", Application.ExecutablePath.ToString());

And I also have a ini.xml file that needs to be in the same folder as my app. So I access it in this manner:

XmlDocument doc = new XmlDocument();
        doc.Load(Application.StartupPath.ToString() + "/ini.xml");

The problem is, after rebooting computer Application.StartupPath is no longer returning my old path but returns C:\Windows\System32\myapp.exe, thus it throws an exception: cannot find the needed ini.xml in that folder. How can I fix this? I need a method that will return REAL path in order to access my ini.xml. Thank you!

  • Your diagnostic certainly doesn't match the way this normally works. Application.StartupPath doesn't change, it is still where ever you copied the EXE file. You could only get c:\windows\system32 if you copied it there. At least document what you see with Regedit.exe when you look at the Run key. – Hans Passant May 21 '13 at 18:15

1 Answers1

1

Don't use the current working directory. Instead use:

Assembly.GetExecutingAssembly().Location

like so:

Path.Combine(
    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
    "myIniFile.ini")
spender
  • 117,338
  • 33
  • 229
  • 351
  • this: Environment.CurrentDirectory = Path.GetDirectoryName(Application.ExecutablePath); on form_load() solved my problem. Thank you anyway! – Carteră Veaceslav May 21 '13 at 18:03