【问题标题】:Process.Start() can't find file? [duplicate]Process.Start() 找不到文件? [复制]
【发布时间】:2020-08-29 21:45:12
【问题描述】:

我正在做一个助手,我想让它打开应用程序。它只打开某些应用程序,它们是 chrome.exe(Chrome) 和 devenv.exe(Visual Studio)。

我还有 4 个应用程序,分别是 Spotify.exe(Spotify)、Discord.exe(Discord)、obs64.exe(OBS Studio) 和 Steam.exe(Steam)。当我告诉助手打开显示以下内容的某个应用时出现错误:

"System.ComponentModel.Win32Exception: '系统找不到指定的文件'".

所有这些应用程序的错误都是相同的。这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Speech.Synthesis;
using System.Speech.Recognition;
using System.Media;
using System.Diagnostics;

namespace Assistant
{
    public partial class Form1 : Form
    {
        SpeechSynthesizer ZeroTwo;
        SpeechRecognitionEngine ZeroTwoEars;
        SoundPlayer ZeroTwoGreeting;
        Grammar ZeroTwoGrammar;

        bool ZeroTwoSearch = false;

        public Form1()
        {
            InitializeComponent();

            // Sound player

            ZeroTwoGreeting = new SoundPlayer(@"c:/Users/User/Desktop/Zero Two Sound Files/dahling ohaio.wav");

            // Speech Synthesizer

            ZeroTwo = new SpeechSynthesizer();      

            // Speech Recognition

            ZeroTwoEars = new SpeechRecognitionEngine();
            Choices list = new Choices();

            list.Add(new string[] { 

                // Greetings

                "Ohaio, zero two",

                // Google search

                "Zero two, search for",

                // Open apps

                "Zero two, open discord",
                "Zero two, open google",
                "Zero two, open visual studio",
                "Zero two, open spotify",
                "Zero two, open steam",
                "Zero two, open obs"
            } );

            ZeroTwoGrammar = new Grammar(new GrammarBuilder(list));

            try
            {

                ZeroTwoEars.RequestRecognizerUpdate();

                ZeroTwoEars.LoadGrammar(ZeroTwoGrammar);

                ZeroTwoEars.SpeechRecognized += ZeroTwoEars_SpeechRecognized;

                ZeroTwoEars.SetInputToDefaultAudioDevice();

                ZeroTwoEars.RecognizeAsync(RecognizeMode.Multiple);

            }
            catch { }

        }
        private void ZeroTwoEars_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            String ZamdiesLines = e.Result.Text;
            button1.Text = ZamdiesLines;

            if (ZeroTwoSearch == true)
            {
                ZeroTwoGreeting.Play();
                Process.Start("https://www.google.com/search?q=" + ZamdiesLines);
                button1.Text = "Searched!";
                ZeroTwoSearch = false;
            }

            if (ZamdiesLines == "Zero two, search for")
            {
                ZeroTwoSearch = true;
                ZeroTwoGreeting.Play();
                button1.Text = "fired!";
            }

            if (ZeroTwoSearch == false)
            {
                switch (ZamdiesLines)
                {


                    case ("Ohaio, zero two"):
                        ZeroTwoGreeting.Play();
                        break;


                    case ("Zero two, open discord"):
                        ZeroTwoGreeting.Play();
                        Process.Start("Discord.exe");
                        break;


                    case ("Zero two, open google"):
                        ZeroTwoGreeting.Play();
                        Process.Start("chrome.exe");
                        break;


                    case ("Zero two, open visual studio"):
                        ZeroTwoGreeting.Play();
                        Process.Start("devenv.exe");
                        break;


                    case ("Zero two, open spotify"):
                        ZeroTwoGreeting.Play();
                        Process.Start("Spotify.exe");
                        break;


                    case ("Zero two, open steam"):
                        ZeroTwoGreeting.Play();
                        Process.Start("Steam.exe");
                        break;


                    case ("Zero two, open obs"):
                        ZeroTwoGreeting.Play();
                        Process.Start("obs64.exe");
                        break;
                }
            }
        }
    }
}

【问题讨论】:

    标签: c# process.start


    【解决方案1】:

    Chrome.exe 和 Devenv.exe 自动运行的原因是,对于这两个应用程序,它们的路径(它们所在的文件夹)都在系统 Path 环境变量中声明。 有两种解决方案。

    1. 您必须通过设置完整路径来调用您的 Discoord.exe 应用程序,例如:

      Process.Start("C:\\Users\\AppData\\Local\\Discord\\app-0.0.307\\Discord.exe");

    2. 在您的计算机系统中设置环境路径。 例如。看下面的截图:

    【讨论】:

      猜你喜欢
      • 2012-10-31
      • 1970-01-01
      • 2019-11-09
      • 2019-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-04
      • 2011-01-23
      相关资源
      最近更新 更多