Application will not launch from command line full path, but will after CDing to directory

c#, executable, path, windows

Solution

Apparently your app depends on the "Current Directory". For opening some sort of file.

The best thing to do is to find that dependency and fix it with an absolute path.

When that's not possible the second-best option would be to change the Current folder to that of the running .EXE as soon as possible. That mean you should execute this line as soon as possible:

Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory;

But that will only work if you manage to execute that before the faulty code. Could be hard when it's for instance in a static constructor.

Problem

I am attempting to get a .Net C# application to run from a Registry `Run` key (at `HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run`, where other string values live and launch things just fine). But for some inexplicable reason, the path to my app doesn't launch my app. So I decided to run the command manually from a command prompt, just to see if that was the problem. It was. So now I'm really perplexed: This is a .Net 4.0 C# application which I've compiled in Release mode. The application lives at: ``` C:\Program Files (x86)\MyCompany\MyProduct\MyProduct.exe ``` I can double-click on the application and it runs properly. I can also open a CMD window and do the following: ``` cd "C:\Program Files (x86)\MyCompany\MyProduct\" MyProduct.exe ``` And the application launches just fine. HOWEVER, if I try this: ``` "C:\Program Files (x86)\MyCompany\MyProduct\MyProduct.exe" ``` The application does not launch. (!) SO obviously the Registry key isn't going to work either. Is there some kind of additional step which must be taken to run a .Net application from its full path?

Original source