Window Service & C#

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I have a window service program created in c#. The window service program is
installed and running. In the mean time, I have another c# application, I
would like to know if there is a way to find out in this c# application where
(the complete path)the window service program is running from. Can someone
provide the sample code?


Thanks.

Potsang
 
I use string
startpath=Path.GetDirectoryName(Environment.CommandLine.Replace('"',' '));
 
Here is a sample code..

Process[] processList = Process.GetProcesses();
string processPath = string.Empty;
//Get the list of current processes running on your machine

foreach (Process proc in processList)
{
//Compare the processName with your process name
if (proc.ProcessName == "YourProcessName")
{
//The MainModule.FileName returns the complete path of
//the exe that it is running from
processPath = proc.MainModule.FileName;
}

}
System.Console.WriteLine(processPath);
 
Here is the more refined code than man previous one...

using System.Diagnostics


Process p = Process.GetProcessesByName("YourProcessName");
string processPath = p.MainModule.FileName;

--
Thinathayalan Ganesan
http://CyberSannyasi.blogspot.com


Thinathayalan Ganesan said:
Here is a sample code..

Process[] processList = Process.GetProcesses();
string processPath = string.Empty;
//Get the list of current processes running on your machine

foreach (Process proc in processList)
{
//Compare the processName with your process name
if (proc.ProcessName == "YourProcessName")
{
//The MainModule.FileName returns the complete path of
//the exe that it is running from
processPath = proc.MainModule.FileName;
}

}
System.Console.WriteLine(processPath);

--
Thinathayalan Ganesan
http://CyberSannyasi.blogspot.com


Potsang said:
Hi,

I have a window service program created in c#. The window service program is
installed and running. In the mean time, I have another c# application, I
would like to know if there is a way to find out in this c# application where
(the complete path)the window service program is running from. Can someone
provide the sample code?


Thanks.

Potsang
 
Hi Potsang,

Thinathayalan's reply has shown how to enumerate all the running processes
to find your "Windows Service" and get its full path. I think it should
meet your need. I want to provide some more information to you.

If you want to retrieve the full path to the "Windows Service" when it is
not running, you can not use System.Diagnostics.Process class solution. In
this requirement, you have to use QueryServiceConfig Win32 API to obtain
the installed service binary full path. However, .Net did not expose this
QueryServiceConfig API from class library, so you have to p/invoke to call
it. You may use the logic below:
1. Use ServiceController.GetServices method to retrieve all the services
installed on your system.
2. Enumerate through this services list and compare
ServiceController.ServiceName property to find your service
ServiceController reference.
3. Then p/invoke QueryServiceConfig win32 API by passing
ServiceControllerServiceHandle.handle to it. The retrieved
QUERY_SERVICE_CONFIG structure has a field of lpBinaryPathName, which
contains the full binary path of the service.

My original reply below contains some more information:
http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/6452
aa87e538615f?hl=zh-CN&

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Potsang said:
Hi,

I have a window service program created in c#. The window service program is
installed and running. In the mean time, I have another c# application, I
would like to know if there is a way to find out in this c# application where
(the complete path)the window service program is running from. Can someone
provide the sample code?


Thanks.

Potsang


Nothing easier than using System.Management for this.


string objPath = "Win32_Service.Name='spooler'";
string servicePath = null;
using(ManagementObject service = new ManagementObject( new ManagementPath(objPath)))
{
servicePath = (string)service.Properties["PathName"].Value;
}
....

Willy.
 
"Jeffrey Tan[MSFT]" said:
Hi Potsang,

Thinathayalan's reply has shown how to enumerate all the running processes
to find your "Windows Service" and get its full path. I think it should
meet your need. I want to provide some more information to you.

If you want to retrieve the full path to the "Windows Service" when it is
not running, you can not use System.Diagnostics.Process class solution. In
this requirement, you have to use QueryServiceConfig Win32 API to obtain
the installed service binary full path. However, .Net did not expose this
QueryServiceConfig API from class library, so you have to p/invoke to call
it. You may use the logic below:
1. Use ServiceController.GetServices method to retrieve all the services
installed on your system.
2. Enumerate through this services list and compare
ServiceController.ServiceName property to find your service
ServiceController reference.
3. Then p/invoke QueryServiceConfig win32 API by passing
ServiceControllerServiceHandle.handle to it. The retrieved
QUERY_SERVICE_CONFIG structure has a field of lpBinaryPathName, which
contains the full binary path of the service.

My original reply below contains some more information:
http://groups.google.com/group/microsoft.public.dotnet.languages.vb/msg/6452
aa87e538615f?hl=zh-CN&

Hope this helps.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


No need to go down the PInvoke route, System.Management contains all classes needed to
perform management tasks like this, isn't the purpose of the FCL to eliminate the need to
call into Win32 just like we do from unmanaged code?

Willy.
 
Hi Willy ,

Oh, thank you for sharing the WMI solution! Yes, Win32_Service should be a
correct solution and by using System.Management we can use WMI in .Net
without p/invoke unmanaged code.

I am always a Win32 API guy, so I seldom thought solution from WMI
perspective :-)

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
"Jeffrey Tan[MSFT]" said:
Hi Willy ,

Oh, thank you for sharing the WMI solution! Yes, Win32_Service should be a
correct solution and by using System.Management we can use WMI in .Net
without p/invoke unmanaged code.

I am always a Win32 API guy, so I seldom thought solution from WMI
perspective :-)

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Jeffrey,

I'm a Win32 guy too, however, System.Management/WMI virtualizes Win32 which guarantees you
to do the right thing. Another great advantage is that you don't need to run in an
administrator account to perform administrative tasks, something you will appreciate when
moving to Vista.

Willy.
 
Back
Top