-1

I am making an assistant and I want it to open applications. It only opens certain applications which are chrome.exe(Chrome) and devenv.exe(Visual Studio).

I have more 4 applications which are Spotify.exe(Spotify), Discord.exe(Discord), obs64.exe(OBS Studio) and Steam.exe(Steam). I get an error when I tell the assistant to open that certain app that says:

"System.ComponentModel.Win32Exception: 'The system cannot find the file specified'".

The error is the same for all of these applications. This is my code:

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;
                }
            }
        }
    }
}
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Zamdie
  • 53
  • 1
  • 5

1 Answers1

1

The reason that Chrome.exe & Devenv.exe are running automatically is that for both applications their paths (folders where they reside) are declared in the system Path environment variable. There are 2 solutions for that.

  1. You have to either call your Discoord.exe application by setting the full path of it, eg:

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

  2. Set the environment Path in the system of your computer. eg. look the bellow screen-shot:

enter image description here