473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Window Service & C#

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
Feb 16 '07 #1
9 2455
I use string
startpath=Path.GetDirectoryName(Environment.Comman dLine.Replace('"',' '));

"Potsang" <po*****@newsgroups.nospamha scritto nel messaggio
news:14**********************************@microsof t.com...
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

Feb 16 '07 #2
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:
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
Feb 16 '07 #3
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:
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:
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
Feb 16 '07 #4
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.

Feb 19 '07 #5
"Potsang" <po*****@newsgroups.nospamwrote in message
news:14**********************************@microsof t.com...
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.

Feb 19 '07 #6
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comwrote in message
news:ld**************@TK2MSFTNGHUB02.phx.gbl...
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.

Feb 19 '07 #7
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.

Feb 20 '07 #8
""Jeffrey Tan[MSFT]"" <je***@online.microsoft.comwrote in message
news:lK**************@TK2MSFTNGHUB02.phx.gbl...
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.

Feb 20 '07 #9
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 ***
Mar 5 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: MichaelC | last post by:
Hi, I'v created NT service using Windows service project template. Now , I want to recieve WM_USERCHANGE and WM_DEICECHANGE messages after the service started. How can i do this? Thanks.
6
by: Siegfried Heintze | last post by:
I have the following C# code in my web service: public static extern double add_(double X, double Y ); public double add (double X, double Y){ return add_(X, Y); }
7
by: =?Utf-8?B?Vmlua2k=?= | last post by:
public void sendKeysTest() { Process myProcess = Process.Start(@"C:\winnt\system32\cmd.exe"); SetForegroundWindow(myProcess.Handle); if (myProcess.Responding) SendKeys.SendWait("{ENTER}");...
1
by: mdhaman | last post by:
hi, I have a windows service written in VB.Net and framework 2.0. It is a multithread service and it is using threadpool to manage threads. Recently I have started getting...
0
by: henkya | last post by:
Language Used: C# Project Typed: Setup Project (for .NET Window Service) Hi Bump into this issue and seriously have a hunch that this is a bug in the .NET setup project. These are the scenario:...
2
by: wreed06 | last post by:
Hello, I have 2 problems. In my webpage, I have a dropdown list with a button that takes the user to a popup window specific to the option. I am using Firefox 2.0.0.13. I have successfully...
2
by: gray d newbie | last post by:
Greetings All, This is my first time creating a Window Service and I am having this error message when I try to start my window service (currently known as Service1). Below is my code for Window...
1
by: vinpkl | last post by:
hi all i have two dynamic drop downs of dealer id and category id which work properly with window.location var dealerid; function getList(xyz) {...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.