我需要在以下代码中进行哪些更改? [英] What changes i need to apply in the following code?
本文介绍了我需要在以下代码中进行哪些更改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是在尝试检查某个特定程序是否正在运行,或者未使用以下代码.但是它始终显示"Not running".我犯了什么错误?请帮助我进行纠正.
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.
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();
}
推荐答案
对于不区分大小写的字符串比较,请使用:
For a non-case-sensitive string comparison, use:
String.Equals( myProcess.ProcessName, "...", StringComparison.OrdinalIgnoreCase );
使用此
Hi,
Use This
foreach (Process myPro in myProcesss)
{
if (myPro.ProcessName.Equals("ConsoleApplication7.exe")
{
Console.WriteLine("Running");
}
else
{
Console.WriteLine("Not Running");
}
}
谢谢...
Thanks...
首先,您将每个进程名解析为大写,然后将其与普通大小写字符串进行比较.那永远不会是真的. Second ProcessName仅返回进程的名称,不带扩展名,也不带目录信息.因此,名为myProgramme.exe
的程序将返回myProgram
而没有扩展名.尝试一些喜欢的事情:-
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 calledmyProgramme.exe
will returnmyProgram
without the extension. Try something liek this:-
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);
显然,用MessageBox代替Console.WriteLine代替控制台应用程序,用myProgramme代替程序名称.
希望对您有帮助
obviously substitute MessageBox for Console.WriteLine for a console app and myProgramme for the name of your programme.
Hope this helps
这篇关于我需要在以下代码中进行哪些更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文