none
How to find the path to the executable file for a service RRS feed

  • Question

  • How can I find the path to the executable file for a service that is listed in the services list? (programatically in C#)

    thx


    • Edited by sjs1978 Wednesday, June 13, 2012 2:17 PM
    Wednesday, June 13, 2012 2:16 PM

Answers

  • I found a solution that works

    http://stackoverflow.com/questions/4555350/get-the-version-information-of-an-installed-service

            private static string GetExecutablePathForService(string serviceName, bool throwErrorIfNonExisting)
            {
                string value = string.Empty;
                string registryPath = @"SYSTEM\CurrentControlSet\Services\" + serviceName;
    
                RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, Environment.MachineName).OpenSubKey(registryPath);
                if (key == null)
                {
                    if (throwErrorIfNonExisting)
                        throw new ArgumentException("Non-existent service: " + serviceName, "serviceName");
                    else
                        return null;
                }
                value = key.GetValue("ImagePath").ToString();
                key.Close();
                if (value.StartsWith("\""))
                {
                    value = Regex.Match(value, "\"([^\"]+)\"").Groups[1].Value;
                }
    
                return Environment.ExpandEnvironmentVariables(value);
            }

    • Marked as answer by sjs1978 Wednesday, June 13, 2012 5:02 PM
    Wednesday, June 13, 2012 5:02 PM

All replies

  • well I found something and it suggests that I must look in the registry? Is this the only way?

    http://social.msdn.microsoft.com/Forums/en-US/csharplanguage/thread/d466ef35-303a-4026-89af-338b874d6faf

    Also if that is true, how do I search for this in the registry using C#?

    Wednesday, June 13, 2012 2:35 PM
  • The service should be running on your PC or registered.  If it is running the windows operating system will automatically connect to the running service.  To test that it is running you could put the service name into a webbrowser URL once the service is registered on the PC.

    jdweng

    Wednesday, June 13, 2012 4:48 PM
  • Hi, 

    Windows services will not have Current Directory is pointing to where it's executables are located but pointing to "System32" folder. that is the folder path you get when you access Environement.CurrentDirectory.

    You can check with, 

    System.AppDomain.CurrentDomain.BaseDirectory
    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

    Wednesday, June 13, 2012 4:53 PM
  • I found a solution that works

    http://stackoverflow.com/questions/4555350/get-the-version-information-of-an-installed-service

            private static string GetExecutablePathForService(string serviceName, bool throwErrorIfNonExisting)
            {
                string value = string.Empty;
                string registryPath = @"SYSTEM\CurrentControlSet\Services\" + serviceName;
    
                RegistryKey key = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, Environment.MachineName).OpenSubKey(registryPath);
                if (key == null)
                {
                    if (throwErrorIfNonExisting)
                        throw new ArgumentException("Non-existent service: " + serviceName, "serviceName");
                    else
                        return null;
                }
                value = key.GetValue("ImagePath").ToString();
                key.Close();
                if (value.StartsWith("\""))
                {
                    value = Regex.Match(value, "\"([^\"]+)\"").Groups[1].Value;
                }
    
                return Environment.ExpandEnvironmentVariables(value);
            }

    • Marked as answer by sjs1978 Wednesday, June 13, 2012 5:02 PM
    Wednesday, June 13, 2012 5:02 PM