473,394 Members | 2,071 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,394 software developers and data experts.

Find if service is running using System.Management

I am trying to determine if a particular process is running. I think I am
probably missing something obvious, but here is what I have so far...

ManagementObjectSearcher msProcessCcmExec = new
ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE Name =
'CcmExec.exe'");

foreach (ManagementObject mo in msProcessCcmExec.Get())
{
logWrite.WriteLine(mo["Name"].ToString() + " is
running.");
}

This works fine if ccmexec.exe is running as it will write the line to my
log file. But what I really want to do is find out if ccmexec.exe is
running. So, how would that best be accomplished? I need to answer the
questions, "Is ccmexec.exe running?".
Thanks in advance for any help.

Jul 9 '06 #1
3 4449
Primera,

If the program wasn't running, then it wouldn't be returned to you in
the query. It's only going to be returned to you if the process is actually
executing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Primera" <Pr*****@discussions.microsoft.comwrote in message
news:0A**********************************@microsof t.com...
>I am trying to determine if a particular process is running. I think I am
probably missing something obvious, but here is what I have so far...

ManagementObjectSearcher msProcessCcmExec = new
ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE Name =
'CcmExec.exe'");

foreach (ManagementObject mo in msProcessCcmExec.Get())
{
logWrite.WriteLine(mo["Name"].ToString() + " is
running.");
}

This works fine if ccmexec.exe is running as it will write the line to my
log file. But what I really want to do is find out if ccmexec.exe is
running. So, how would that best be accomplished? I need to answer the
questions, "Is ccmexec.exe running?".
Thanks in advance for any help.

Jul 9 '06 #2
Yes, that makes sense. Either way though I am left with an object of type
ManagementObjectSearcher or if I use a foreach an object of type
ManagementObject. So then I seem to be running into a problem testing if
these object are empty so that I can answer the question of whether the
process is running. I hope this makes sense. What type of statement can I
use to discern if the ManagementObjectSearcher is empty? I have tried using:

if (msProcessCcmExec != null)

but this returns an error about casting to bool, and I cannot manually cast
it to bool either. I appreciate your help.

"Nicholas Paldino [.NET/C# MVP]" wrote:
Primera,

If the program wasn't running, then it wouldn't be returned to you in
the query. It's only going to be returned to you if the process is actually
executing.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Primera" <Pr*****@discussions.microsoft.comwrote in message
news:0A**********************************@microsof t.com...
I am trying to determine if a particular process is running. I think I am
probably missing something obvious, but here is what I have so far...

ManagementObjectSearcher msProcessCcmExec = new
ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE Name =
'CcmExec.exe'");

foreach (ManagementObject mo in msProcessCcmExec.Get())
{
logWrite.WriteLine(mo["Name"].ToString() + " is
running.");
}

This works fine if ccmexec.exe is running as it will write the line to my
log file. But what I really want to do is find out if ccmexec.exe is
running. So, how would that best be accomplished? I need to answer the
questions, "Is ccmexec.exe running?".
Thanks in advance for any help.


Jul 9 '06 #3
I think I have solved this using:

ManagementObjectSearcher msProcessCcmSetup = new ManagementObjectSearcher("SELECT
* FROM Win32_Process WHERE Name = 'CcmSetup.exe'");
ManagementObjectCollection moCcmSetup = msProcessCcmSetup.Get();
if (moCcmSetup.Count == 0)
{
logWrite.WriteLine(moCcmSetup.Count);
logWrite.WriteLine("CcmSetup.exe is not running.");
}
if (moCcmSetup.Count != 0)
{
foreach (ManagementObject mo in moCcmSetup)
{
logWrite.WriteLine(mo["Name"] + " is running.");
}
}
I am trying to determine if a particular process is running. I think
I am probably missing something obvious, but here is what I have so
far...

ManagementObjectSearcher msProcessCcmExec = new
ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE Name =
'CcmExec.exe'");

foreach (ManagementObject mo in
msProcessCcmExec.Get())
{
logWrite.WriteLine(mo["Name"].ToString() + " is
running.");
}
This works fine if ccmexec.exe is running as it will write the line to
my
log file. But what I really want to do is find out if ccmexec.exe is
running. So, how would that best be accomplished? I need to answer
the
questions, "Is ccmexec.exe running?".
Thanks in advance for any help.

Jul 9 '06 #4

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

Similar topics

2
by: Mikael Sorensen | last post by:
I've written a windows service (VB.NET 2003). When running, it consumes between 20-40 MB memory. How do I optimize the memory usage? I've set all objects to Nothing when I'm done using them. ...
2
by: Mark | last post by:
I created an extremely simple windows service that only writes to the EventLogs on Stop and Pause. I installed it using the InstallUtil.exe program, the output of which is below. It appears to be...
0
by: MaxBici | last post by:
Hi, I have a windows service in C# (.NET framework 1.1) running on Windows XP PRO SP2. This service essentiallt starts a printjob watcher. Please see the code snippet below. queryInterval =...
9
by: Rotzooi | last post by:
Hi, I have a VB.NET Service application that's running fine under the Local System account. But for configuration purposes I don't want to be dependent on modifying the registry manually or...
3
by: gerard.mears | last post by:
Hi, I'm a total newbie at VB.NET so please bare with me. I would like to create a simple service that will log all exe and com files that are executed but I don't know where to start. I'm...
2
by: Soren S. Jorgensen | last post by:
Hi, How do I see, from within my main method, in what context my assembly, containing my service program, is executed ?? I want to be able to detect if the assembly is executed by the service...
33
by: JamesB | last post by:
I am writing a service that monitors when a particular app is started. Works, but I need to get the user who is currently logged in, and of course Environment.UserName returns the service logon...
6
by: tsmojver | last post by:
Does anyone know how to retreive the logon account username for a particular windows service? Or the current user that is running this service as a process. C# is the prefered language of choice. ...
3
by: =?Utf-8?B?UVNJRGV2ZWxvcGVy?= | last post by:
I have a Windows service that we need to have multiple instances running. We achieve this by using InstallUtil Service.exe /name="SomeServiceName". I need to know the name of the instance of the...
7
by: sachinkale123 | last post by:
I want to stop Sql server Agent Windows service. I am using below code to stop or start the windows service. Which is I am doing sucessfully. ManagedComputer mc = default(ManagedComputer); ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.