Process.Start () ไม่พบไฟล์? [ซ้ำ]

Aug 30 2020

ฉันกำลังสร้างผู้ช่วยและต้องการให้เปิดแอปพลิเคชัน เปิดเฉพาะบางแอปพลิเคชันซึ่ง ได้แก่ 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;
                }
            }
        }
    }
}

คำตอบ

1 EfthymiosKalyviotis Aug 30 2020 at 06:17

สาเหตุที่ Chrome.exe & Devenv.exe ทำงานโดยอัตโนมัติสำหรับทั้งสองแอปพลิเคชันเส้นทาง (โฟลเดอร์ที่อยู่) จะถูกประกาศในPathตัวแปรสภาพแวดล้อมของระบบ มี 2 ​​วิธีแก้ปัญหาสำหรับสิ่งนั้น

  1. คุณต้องเรียกใช้แอปพลิเคชัน Discoord.exe โดยกำหนดเส้นทางแบบเต็มเช่น:

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

  2. ตั้งค่าเส้นทางสภาพแวดล้อมในระบบคอมพิวเตอร์ของคุณ เช่น. ดูภาพหน้าจอร้อง: