Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I was just trying to check a particular program is currently running or not using the following code.But it always showing "Not running".What is the mistake ihave done?Kindly help me to rectify it.
C#
static void Main(string[] args)
{
    Process[] myProcesss;
    myProcesss = Process.GetProcesses();
    foreach (Process myProcess in myProcesss)
    {
        if (myProcess.ProcessName.ToUpper() == "C:\\MyWebServerroot\\ConsoleApplication7\\ConsoleApplication7\\bin\\Debug\\ConsoleApplication7.exe")
        {
            Console.WriteLine("Running");
        }
        else
        {
            Console.WriteLine("Not Running");
        }
    }
    Console.ReadLine();
}
Posted

For a non-case-sensitive string comparison, use:

String.Equals( myProcess.ProcessName, "...", StringComparison.OrdinalIgnoreCase );
 
Share this answer
 
Hi,

Use This
C#
foreach (Process myPro in myProcesss)
    {
        if (myPro.ProcessName.Equals("ConsoleApplication7.exe")
        {
            Console.WriteLine("Running");
        }
        else
        {
            Console.WriteLine("Not Running");
        }
    }


Thanks...
 
Share this answer
 
First of all, you parse each processname to upper case and then compare it to a normal case string. That won't ever be true. Second ProcessName returns the name of the process only, without the extension, and without the directory info. Thus a programme that is called myProgramme.exe will return myProgram without the extension. Try something liek this:-


C#
Process[] myProcesss;
           myProcesss = Process.GetProcesses();
           bool isRunning = false;
           foreach (Process myProcess in myProcesss)
           {
               if (myProcess.ProcessName.Equals("myProgramme",StringComparison.InvariantCultureIgnoreCase ))
               {
                   isRunning = true;
               }
           }
           string message = isRunning ? "Running" : "Not Running";
           MessageBox.Show(message);


obviously substitute MessageBox for Console.WriteLine for a console app and myProgramme for the name of your programme.

Hope this helps
 
Share this answer
 
v3
Comments
sreenathpktr 18-Oct-11 7:22am    
Thanks for the reply.I applied your code.But it shoes the "Running" only..
Wayne Gaylard 18-Oct-11 7:40am    
I made an error in the if shorthand ( ? : ). I have updated my answer including a way to ensure there are no case errors.
sreenathpktr 18-Oct-11 7:51am    
Thanks for the updation...But now also it always shows "Not Runnng"..
Wayne Gaylard 18-Oct-11 8:38am    
Have you checked in task manager that the process is running ? and how it is spelt ? Did you substitute myProgramme for your process name ? I have checked the code I posted and it works properly, so I think the issue is with one of those I mentioned.
sreenathpktr 18-Oct-11 9:04am    
Yes..i checked everything...this is the code i have applied.And still it's showing "Not Running"..
static void Main(string[] args)
{
Process[] myProcesss;
myProcesss = Process.GetProcesses();
bool isRunning = false;
foreach (Process myProcess in myProcesss)
{
if (myProcess.ProcessName.Equals("ConsoleApplication7", StringComparison.InvariantCultureIgnoreCase))
{
isRunning = true;
}
}
string message = isRunning ? "Running" : "Not Running";
Console.WriteLine(message);
Console.ReadLine();
}
Could you tell me what is the error?
It might help you,

C#
class Program
    {
        static void Main(string[] args)
        {
            string processToCheck = "???????.exe";
            if (ProcessCheck(processToCheck))
            {
                Console.WriteLine("Running");
            }
            else
            {
                Console.WriteLine("Not Running");
            }
        }

        private static bool ProcessCheck(string processToCheck)
        {
            return Process.GetProcesses().Where(process => process.ProcessName.Equals(processToCheck.Remove(processToCheck.IndexOf('.')), StringComparison.OrdinalIgnoreCase)).Count() > 0;
        }
    }

:)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900