Process.Start() 找不到文件?
Process.Start() can't find file?
我正在做一个助手,我想让它打开应用程序。它只打开某些应用程序,即 chrome.exe(Chrome) 和 devenv.exe(Visual Studio).
我还有 4 个应用程序,它们是 Spotify.exe(Spotify)、Discord.exe(Discord)、obs64.exe(OBS Studio) 和 Steam.exe(Steam)。当我告诉助手打开某个应用程序时,我收到一条错误消息:
"System.ComponentModel.Win32Exception: 'The system cannot find the file specified'".
所有这些应用程序的错误都是一样的。这是我的代码:
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;
}
}
}
}
}
Chrome.exe 和 Devenv.exe 自动 运行 的原因是,对于这两个应用程序,它们的路径(它们所在的文件夹)是在系统 Path
环境中声明的多变的。
有 2 个解决方案。
您必须通过设置 的完整路径来调用您的 Discoord.exe 应用程序,例如:
Process.Start("C:\\Users\\AppData\\Local\\Discord\\app-0.0.307\\Discord.exe");
在您的电脑系统中设置环境路径。 例如。看下面的 screen-shot:
我正在做一个助手,我想让它打开应用程序。它只打开某些应用程序,即 chrome.exe(Chrome) 和 devenv.exe(Visual Studio).
我还有 4 个应用程序,它们是 Spotify.exe(Spotify)、Discord.exe(Discord)、obs64.exe(OBS Studio) 和 Steam.exe(Steam)。当我告诉助手打开某个应用程序时,我收到一条错误消息:
"System.ComponentModel.Win32Exception: 'The system cannot find the file specified'".
所有这些应用程序的错误都是一样的。这是我的代码:
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;
}
}
}
}
}
Chrome.exe 和 Devenv.exe 自动 运行 的原因是,对于这两个应用程序,它们的路径(它们所在的文件夹)是在系统 Path
环境中声明的多变的。
有 2 个解决方案。
您必须通过设置 的完整路径来调用您的 Discoord.exe 应用程序,例如:
Process.Start("C:\\Users\\AppData\\Local\\Discord\\app-0.0.307\\Discord.exe");
在您的电脑系统中设置环境路径。 例如。看下面的 screen-shot: