为插入的命令或URL获取Win32Exception

时间:2016-03-25 23:53:45

标签: c#

我正在使用语音识别系统来打开带书签的命令或网址。我面临一个未知的win32exception错误?在代码中设置固定的单词可以很好地工作,但不适用于插入文本字段的命令。

这是我的代码:

namespace Voice_Bookmarking_System
{
  public partial class Form1 : Form
  {
    SpeechRecognitionEngine recEngine = new SpeechRecognitionEngine();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        var frm1 = new Design();
        frm1.ShowDialog(this); // make sure this instance of Form1 is visible
        label1.Text = frm1.t1;
        label2.Text = frm1.r1;
        LoadGrammars();
        StartRecognition();
    }

    private void LoadGrammars()
    {
        Choices commands = new Choices();
        commands.Add(new string[] { "'" + label1.Text + "'", "a",});
        GrammarBuilder grammarbuilder = new GrammarBuilder();
        grammarbuilder.Append(commands);
        Grammar grammar = new Grammar(grammarbuilder);
        recEngine.LoadGrammarAsync(grammar);
    }

    private void StartRecognition()
    {
        recEngine.SpeechDetected += new EventHandler<SpeechDetectedEventArgs>(recEngine_SpeechDetected);
        recEngine.SpeechRecognitionRejected += new EventHandler<SpeechRecognitionRejectedEventArgs>(recEngine_SpeechRecognitionRejected);
        recEngine.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recEngine_SpeechRecognized);
        recEngine.RecognizeCompleted += new EventHandler<RecognizeCompletedEventArgs>(recEngine_RecognizeCompleted);

        Thread t1 = new Thread(delegate ()
        {
            recEngine.SetInputToDefaultAudioDevice();
            recEngine.RecognizeAsync(RecognizeMode.Single);
        });
        t1.Start();
    }

    private void recEngine_SpeechDetected(object sender, SpeechDetectedEventArgs e)
    {
        textBox1.Text = "Recognizing voice command...";
    }

    private void recEngine_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        if (e.Result.Text.ToLower() == "'" + label1.Text + "'")
        {
            try
            {
                Process.Start("'" + label2.Text + "'");
            }
            catch (Win32Exception)
            {
                Process.Start("IExplore.exe", "'"+label2.Text+"'");
            }
        }
        switch (e.Result.Text)
        {
            case "a":
            {
                Process.Start("http://www.google.com");
                break;
            }
        }
        textBox1.Text = e.Result.Text;
    }

    private void recEngine_SpeechRecognitionRejected(object sender, SpeechRecognitionRejectedEventArgs e)
    {
        textBox1.Text = "Failure. Please try again!";
    }

    private void recEngine_RecognizeCompleted(object sender, RecognizeCompletedEventArgs e)
    {
        recEngine.RecognizeAsync();
    }
  }
}

这行我得到了win32exception错误:

    Process.Start("IExplore.exe", "'"+label2.Text+"'");

消息显示如下:

类型&#39; System.ComponentModel.Win32Exception&#39;的例外情况发生在System.dll中但未在用户代码中处理

附加信息:系统找不到指定的文件

1 个答案:

答案 0 :(得分:0)

你不需要在任何引用的文本周围包装它。如果你熟悉的话 string.Format函数你可以做以下

entity.Modified
如果你想使用Process.Start("IExplore.exe", "google.com"); //works if hard coded var procToStart = label2.Text; Process.Start("IExplore.exe", procToStart);//if you want to pass it by variable 函数

,那么更高级的东西
string.Format()