我有一个代码,当我说打开一个程序时,它会打开.但是,如果该程序不存在怎么办?如何让我的代码告诉我它不存在?而且,如果存在替代程序,我怎样才能使我的代码打开?
case "open microsoft word":
System.Diagnostics.Process.Start(@"C:\Program Files\Microsoft Office\Office15\WINWORD.exe");
JARVIS.Speak("Loading");
break;
Run Code Online (Sandbox Code Playgroud)
这是一种检查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");
}
}
Run Code Online (Sandbox Code Playgroud)