首页 > 解决方案 > 如何在 C# 中自动搜索和启动 exe?

问题描述

我正在 C# Visual Studio 2019 中编写一个应用程序,该应用程序将为用户启动 Microsoft Word。但是,我正在努力让应用程序搜索单词。我知道 word 安装在基于 64 位或 32 位 Windows、word 的确切版本等的不同位置。所以我通过尝试搜索 winword.exe 的程序文件来处理这个问题,然后在找到时启动它。如果找不到它,它会启动记事本。

              string rootDirectory = System.IO.DriveInfo.GetDrives()[0].RootDirectory.FullName;

            string [] files = System.IO.Directory.GetFiles(
        Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles),
        "WINWORD.EXE", System.IO.SearchOption.AllDirectories);


            Process.Start(files);


        }
        catch (Exception e)
        {
            Console.WriteLine("I could not find word, so launching notepad.");
            Process.Start("C:\\WINDOWS\\system32\\notepad.exe");

它使用字符串文件搜索单词,然后 process.start(stringname) 在找到时启动单词,但它返回此错误。任何想法将不胜感激,非常感谢。

在此处输入图像描述

标签: c#console

解决方案


推荐阅读