如果Microsoft Outlook已经打开,如何使用C#进行检查?

Pig*_*Pie 1 c# outlook email-integration

我试过寻找这个问题的答案......如果我忽略了它,请原谅我.

我想要做的是自动发送电子邮件.我在此代码中拥有我想要的所有内容,但代码假定Outlook未打开.

有没有办法在Outlook打开另一个Outlook实例之前测试Outlook是否已打开?

                Microsoft.Win32.RegistryKey key =
           Microsoft.Win32.Registry.LocalMachine.OpenSubKey("Software\\microsoft\\windows\\currentversion\\app paths\\OUTLOOK.EXE");
            string path = (string)key.GetValue("Path");
            if (path != null)
                System.Diagnostics.Process.Start("OUTLOOK.EXE");
            else
                MessageBox.Show("There is no Outlook in this computer!", "SystemError", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Run Code Online (Sandbox Code Playgroud)

小智 6

int procCount = 0;
Process[] processlist = Process.GetProcessesByName("OUTLOOK");
foreach (Process theprocess in processlist)
{
    procCount++;            
}
 if (procCount > 0)
{
  //outlook is open
}
Run Code Online (Sandbox Code Playgroud)

  • 不需要计数器,你可以只执行 processlist.length > 0 (2认同)

小智 6

因为我喜欢干净的单行,所以我使用的是:

If (System.Diagnostics.Process.GetProcessesByName("OUTLOOK").Any())
  return true;
Run Code Online (Sandbox Code Playgroud)


Ham*_*one 5

这是测试 Outlook 是否打开的一种方法,如果是,则“获取”当前实例。在 catch 块中,您可以按照列出的方式打开新实例:

Outlook.Application ol;

try
{
    ol = (Outlook.Application)Marshal.GetActiveObject("Outlook.Application");
}
catch (Exception ex)
{
    // open your new instance
}
Run Code Online (Sandbox Code Playgroud)