Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to determine an application's location?

I'm writing a windows service in C# that spawns multiple instances of another application I am writing. There is a chance that the application can be installed anywhere on a machine. What is the best way to let the service know where the application is located?

like image 957
MGSoto Avatar asked Jul 14 '09 21:07

MGSoto


People also ask

How is computer location determined?

When the device location setting is enabled, the Microsoft location service will use a combination of global positioning service (GPS), nearby wireless access points, cell towers, and your IP address to determine your device's location.

How do I find the location of an application file?

To find out where an app is installed, the easiest method is to go through a shortcut used to open it. to do so, right-click on a shortcut and select “Open file location”. If the shortcut is in File Explorer, or on the desktop, this will take you straight to the location of the app.

Where are uninstallers located Windows?

In the search box on the taskbar, type Control Panel and select it from the results. Select Programs > Programs and Features. Press and hold (or right-click) on the program you want to remove and select Uninstall or Uninstall/Change. Then follow the directions on the screen.

How do I find a game directory?

One way to determine the game directory of your game is to look for a file called GameInfo. txt . If you find the GameInfo. txt file, then the directory it sits in is the game directory.


2 Answers

If you need to locate the folder your service is installed to you can use the following code

this.GetType().Assembly.Location

If you need to locate the folder some other application is installed to you should make a request to windows installer

[DllImport("MSI.DLL", CharSet = CharSet.Auto)]
private static extern UInt32 MsiGetComponentPath(
    string szProduct,
    string szComponent,
    StringBuilder lpPathBuf,
    ref int pcchBuf);

private static string GetComponentPath(string product, string component)
{
    int pathLength = 1024;
    StringBuilder path = new StringBuilder(pathLength);
    MsiGetComponentPath(product, component, path, ref pathLength);
    return path.ToString();
}
like image 194
Mike Avatar answered Nov 15 '22 06:11

Mike


If you mean that the service starts a different app, then; options:

  • configure the service with a config file; put the path in there
  • put something in the registry during installation
  • use something akin to COM/COM+ registrations
  • consider the GAC if the other app is .NET (although I'm not a fan...)
  • environment variable?

Personally, I like the config file option; it is simple and easy to maintain, and allows multiple separate (side-by-side) service and app installs

like image 27
Marc Gravell Avatar answered Nov 15 '22 07:11

Marc Gravell