简体   繁体   中英

Prevent Application from running inside the wrong folder

I have a (rather huge) application which is depending on some resources residing in the same folder as the application (eg settings files, unmanaged libraries etc.).
I also have registered a URI-protocol for this application, so that every URI-call starting with xyz://... will launch my application with the respective parameters.

The problem is, that if you use the URI-call, the application will run from the %windir%/system32 -folder, or the ../ProgramFiles/Chrome/... -folder (when the URI is called from my webbrowser). It will not run my application from the folder, which it resides in.

(you can probably see my problem already...)

If my app runs from the sys32-folder it will (naturally) not have access to the local libraries, which some of my P/Invoke functions need.


My question is: how can I run my application from the correct folder or prevent it being run from a folder like sys32?


And no - I do not like following idea:

 if (Directory.GetCurrentDirectory().ToLower() != "/my/home/dir") { MessageBox.Show("You have no power over here!"); Exit(1); } 

OK - this comment written by @MutantNinjaCodeMonkey helped me out in the end:

Directory.SetCurrentDirectory(@"c:\path\to\app");

"resets" the working directory to the desired path. In my specific case I used:

FileInfo nfo = new FileInfo(Assembly.GetExecutingAssembly().Location);

Directory.SetCurrentDirectory(nfo.Directory.FullName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM