使用 GetProcessesByName 是检查进程是否正在运行的最佳方法吗?
尽管主题标题解释了大部分问题,但我还是想勾勒出这个场景,以便您了解这个问题是在什么背景下提出的。
我有一个类似于 Outlook 联系人列表的应用程序。它从 Outlook 获取所有联系人,结果是它们显示在数据网格视图中。现在,我不想在每次打开应用程序时启动 Outlook 并将其关闭,而是希望在应用程序已打开时不要打开 Outlook,并在应用程序关闭且用户已运行 Outlook 时保持打开状态。这是我的用法:
Process[] pName = Process.GetProcessesByName("OUTLOOK");
if (pName.Length == 0)
{
MessageBox.Show("Outlook is not running."); // Open Outlook anew.
}
else
{
MessageBox.Show("Outlook is running."); // Do not re-open Outlook.
}
这是最好、最安全的方法吗?先感谢您。
Even though the topic title explains most of the question, I'd like to sketch out the scenario so you understand in what context this question is put.
I have an application which is like an Outlook contacts list. It gets all the contacts from Outlook and the result is that they're displayed in a data grid view. Now, instead of starting Outlook every time my application opens and shutting it down, I want it not to open when it is already open and stay open when my application shuts down and the user already had Outlook running. Here's my usage:
Process[] pName = Process.GetProcessesByName("OUTLOOK");
if (pName.Length == 0)
{
MessageBox.Show("Outlook is not running."); // Open Outlook anew.
}
else
{
MessageBox.Show("Outlook is running."); // Do not re-open Outlook.
}
Is this the best and safest way of doing it? Thank you in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。我想不出更好的办法了。
Yes. I can think of no better way.