0

我有一个代码,当我说打开一个程序时,它就会打开。但是,如果该程序不存在怎么办?如何让我的代码告诉我它不存在?而且,如果存在,我怎样才能让我的代码打开一个替代程序?

case "open microsoft word":
                System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office\Office15\WINWORD.exe");
                JARVIS.Speak("Loading");
                break;
4

2 回答 2

5

这是检查是否安装了 Word的一种简单方法。如果用户将它安装在不同的路径怎么办?它是否需要是特定版本的 Office?我认为你最好检查一下注册表。

using Microsoft.Win32;

// Check whether Microsoft Word is installed on this computer,
// by searching the HKEY_CLASSES_ROOT\Word.Application key.
using (var regWord = Registry.ClassesRoot.OpenSubKey("Word.Application"))
{
    if (regWord == null)
    {
        Console.WriteLine("Microsoft Word is not installed");
    }
    else
    {
        Console.WriteLine("Microsoft Word is installed");
    }
}
于 2013-09-16T01:42:23.973 回答
3

您可以使用File.Exists

if (File.Exists(@"C:\Program Files\Microsoft Office\Office15\WINWORD.exe")) {
    // do your thing
}
于 2013-09-16T01:33:18.007 回答