2016-03-25 73 views
0

我正在做一個語音識別系統來打開書籤命令或網址。我面臨一個未知的win32exception錯誤?在代碼中設置固定字符完美,但不適用於插入文本字段的命令。獲取Win32Exception插入的命令或URL

這裏是我的代碼:

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+"'"); 

消息是這個樣子:

型系統的」異常。 ComponentModel.Win32Exception'發生在System.dll中但未在用戶代碼中處理

其他信息:系統找不到指定的文件

+1

了'label2.Text'的值是一個文本..更多先進的,當你通過'label2.Text'不帶引號那麼會發生什麼。 。 'Process.Start( 「IExplore.exe」,label2.Text);'然後會發生什麼......? – MethodMan

+0

轉換ToString? Unicode字符我的意思是... –

+0

什麼是'label2.Text'的瓦利?..? – MethodMan

回答

0

你不需要包裝這周圍的任何引用文本..如果你熟悉 的String.Format功能,你可以做以下

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()功能

Process.Start("IExplore.exe",string.Format("{0}", procToStart));