开发者

How would I find an exe's path just by knowing its name?

开发者 https://www.devze.com 2023-01-27 22:31 出处:网络
How Would I find another exe\'s path by knowing its name in .net? Wou开发者_如何学编程ld I add name to the OS environment variable?

How Would I find another exe's path by knowing its name in .net?

Wou开发者_如何学编程ld I add name to the OS environment variable? Would the other application have to 'register' itself somewhere else?

I need App A to start-up App B and call some WCF services on it.

Thanks!


To answer your question: you cannot know the path simply by knowing the name. An exe can reside anywhere on the file system. There can be multiple instances of it that don't know about each other. Multiple exe files that are completely different can have the same name.

You could take one of several approaches to get round this, depending on the exe you are targetting:

  • get the user to browse for the exe using a normal file browse dialog
  • search the file system
  • see what traces the target exe leaves on the system (filesystem, registry, environmental variables, etc) and use those traces to locate the exe

For either of these options you save the result so you don't have to execute it again when your app is run the next time.

Searching the filesystem could take some time, you are not guaranteed to find the exe (depending upon the user level your app is running as) and you may get false positives, especially if the app is called something dumb like setup.exe.

Getting the user to locate the exe the first time you run is possibly the most reliable way of locating it, but then you have to decide what to do if your app runs but the target exe is no longer at the specified location, or the user has chosen the wrong exe.

If you have some control over App B (i.e. it is your product), then you could consider adding some info to a known spot in the registry when App B gets installed, so that App A can locate it easily. You still need to have a plan B though in case the info is missing.


Reference a path to a shortcut of the exe in the config setting, that way if the exe ever moves around the shortcut will still be up-to-date. Try it, make a shortcut to a exe, then cut and paste the exe somewhere else, then double click the shortcut and you'll see it points to the exe's new location thus will not require changes to app A if the location app B changes.


Really, just make the App B a windows service and start it up when needed.

UPDATE:

Another suggestion would be to create a hard link to the AppB's EXE:

mklink /H AppB-link.exe path_to_actual_exe

Or a symbolic link to whole directory where App B resides:

mklink /D virtual_directory path_to_actual_directory
0

精彩评论

暂无评论...
验证码 换一张
取 消