我如何确定ms outlook进程是否正在运行 [英] how do i determine if ms outlook process is running
问题描述
我想在发送电子邮件之前确定ms outlook是否正在运行,但我似乎无法正确使用它。我的代码如下所示。欢迎任何帮助
bool msOutlook = false < /跨度>;
foreach (处理otlk Process.GetProcesses())
{
if (otlk.ProcessName.Contains( outlook ))
{
msOutlook = true ;
}
}
if (msOutlook.Equals( true ) )
{
Mail.Send();
MessageBox.Show( 申请人已被转发, Forward Applicant,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MessageBox.Show( 未转发CV。需要运行Ms Outlook实例才能发送电子邮件, < span class =code-string> Mail error,MessageBoxButtons.OK,MessageBoxIcon.Error);
return ;
}
来吧,分配和检查布尔的另一种奇怪方式!
而不是
if (otlk .ProcessName.Contains( outlook))
{
msOutlook = < span class =code-keyword> true ;
}
// ...
if (msOutlook.Equals( true )){ // ......哇! - SA
必须是:
< pre lang =cs> bool isOutlook;
// ...
isOutlook = otlk.ProcessName .Contains( outlook);
// ...
< span class =code-keyword> if (isOutlook) // ...
这只是一个副节点:上面这么高的代码质量表明了布尔的混淆,简直是不可接受的。
现在,问题可能只是字符串的情况。
尝试改为:
isOutlook = otlk.ProcessName.ToLower()。包含( outlook);
毕竟,运行Outlook,在调试器下运行此代码,并确定进程表示Outlook手动通过调试器查找变量。浏览Process
的这个实例,找出与其他进程有什么不同,建立相应的检查。
-SA
您可以简化代码:
if (Process.GetProcessesByName( Outlook.exe )。长度> 0 )
{
Mail 。发送();
MessageBox.Show( 申请人已被转发, Forward Applicant,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MessageBox.Show( 未转发CV。需要运行Ms Outlook实例才能发送电子邮件, < span class =code-string> Mail error,MessageBoxButtons.OK,MessageBoxIcon.Error);
return ;
}
i am trying to determine if ms outlook is running before sending an email but i cant seem to get it right. my code is shown below. Any help will be welcomed
bool msOutlook = false;
foreach(Process otlk in Process.GetProcesses())
{
if(otlk.ProcessName.Contains("outlook"))
{
msOutlook = true;
}
}
if (msOutlook.Equals(true))
{
Mail.Send();
MessageBox.Show("Applicant has been forwarded", "Forward Applicant", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("CV not forwarded. An instance of Ms Outlook needs to be running to send email", "Mail error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Come on, another weird way of assigning and checking Boolean!
Instead of
if (otlk.ProcessName.Contains("outlook")) { msOutlook = true; } //... if (msOutlook.Equals(true)) { //... wow! -- SA
must be:
bool isOutlook; //... isOutlook = otlk.ProcessName.Contains("outlook"); //... if (isOutlook) //...
This is just a side node: such quality of code as above demonstrates confusion about Boolean and simply unacceptable.
Now, the problem can be simply the case of the string.
Try instead:
isOutlook = otlk.ProcessName.ToLower().Contains("outlook");
After all, run Outlook, run this code under debugger, and identify the process representing Outlook "manually" looking the variable through debugger. Browse this instance of theProcess
and find out what differs it from other processes, build you check accordingly.
—SA
You may simplify your code like way:
if (Process.GetProcessesByName("Outlook.exe").Length > 0) { Mail.Send(); MessageBox.Show("Applicant has been forwarded", "Forward Applicant", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show("CV not forwarded. An instance of Ms Outlook needs to be running to send email", "Mail error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; }
这篇关于我如何确定ms outlook进程是否正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!