Connecting Tech Pros Worldwide Forums | Help | Site Map

Window Service & C#

=?Utf-8?B?UG90c2FuZw==?=
Guest
 
Posts: n/a
#1: Feb 16 '07
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

Laura T.
Guest
 
Posts: n/a
#2: Feb 16 '07

re: Window Service & C#


I use string
startpath=Path.GetDirectoryName(Environment.Comman dLine.Replace('"',' '));

"Potsang" <potsang@newsgroups.nospamha scritto nel messaggio
news:14D03800-5BF9-4E1D-866D-13CC470D7E7D@microsoft.com...
Quote:
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

=?Utf-8?B?VGhpbmF0aGF5YWxhbiBHYW5lc2Fu?=
Guest
 
Posts: n/a
#3: Feb 16 '07

re: Window Service & C#


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" wrote:
Quote:
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
=?Utf-8?B?VGhpbmF0aGF5YWxhbiBHYW5lc2Fu?=
Guest
 
Posts: n/a
#4: Feb 16 '07

re: Window Service & C#


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" wrote:
Quote:
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" wrote:
>
Quote:
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
Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
#5: Feb 19 '07

re: Window Service & C#


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/micro...es.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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Willy Denoyette [MVP]
Guest
 
Posts: n/a
#6: Feb 19 '07

re: Window Service & C#


"Potsang" <potsang@newsgroups.nospamwrote in message
news:14D03800-5BF9-4E1D-866D-13CC470D7E7D@microsoft.com...
Quote:
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.

Willy Denoyette [MVP]
Guest
 
Posts: n/a
#7: Feb 19 '07

re: Window Service & C#


""Jeffrey Tan[MSFT]"" <jetan@online.microsoft.comwrote in message
news:ldSWb$8UHHA.3756@TK2MSFTNGHUB02.phx.gbl...
Quote:
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/micro...es.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/subscripti...ult.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/subscripti...t/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.



Jeffrey Tan[MSFT]
Guest
 
Posts: n/a
#8: Feb 20 '07

re: Window Service & C#


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/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Willy Denoyette [MVP]
Guest
 
Posts: n/a
#9: Feb 20 '07

re: Window Service & C#


""Jeffrey Tan[MSFT]"" <jetan@online.microsoft.comwrote in message
news:lKVgGfJVHHA.3604@TK2MSFTNGHUB02.phx.gbl...
Quote:
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/subscripti...ult.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/subscripti...t/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.

Net Guy
Guest
 
Posts: n/a
#10: Mar 5 '07

re: Window Service & C#


The easiest way to get the path to a service.

/// <summary>
/// Fully-qualified path to the service binary file that
implements the
/// service
/// </summary>
/// <param name="serviceName">string</param>
/// <returns>string</returns>
public static string GetPathName(string serviceName)
{
string buffer;
ConnectionOptions _co = null;
ManagementScope _ms = null;
ManagementObject _mo = null;
try
{
_co = new ConnectionOptions();
_co.Impersonation = ImpersonationLevel.Impersonate;
_ms = new ManagementScope(@"root\CIMV2", _co);
_ms.Connect();

_mo = new
ManagementObject(String.Format("Win32_Service.Name ='{0}'",
serviceName));

buffer =
Convert.ToString(_mo.GetPropertyValue("PathName")) ;
}
finally
{
_mo.Dispose();
_mo = null;

if (_ms != null)
{
_ms = null;
if (_co != null)
{
_co = null;
}
}
}
return buffer;
}


*** Sent via Developersdex http://www.developersdex.com ***
Closed Thread