2017-06-23 98 views
0

我正在使用cmd打開解壓縮文件的程序。在我的ToolStripMenu中,我製作了一個WarningToggleComboBox和一個DebugToggleComboBox。如果語句不工作,C#ToolStripComboBox?

所以在另一個類中,我做了一個函數,它將運行cmd並獲取錯誤並在RichTextBox中輸出。

上面的錯誤我做了一個If語句,說明如果DebugToggleComboBox =「On」,然後附加特定的文本到RichTextBox。

在發出錯誤之後,我發出了一條If語句,聲明如果WarningToggleComboBox =「Off」,它將搜索包含隨機整數的錯誤中的特定行,並用空字符串替換警告文本。

問題是,我每次選擇DebugToggleComboBox值爲「On」,並選擇WarningToggleComboBox爲「Off」時,它將輸出警告5次而不是用空字符串替換它,但是當DebugToggleComboBox值爲「關「並且WarningToggleComboBox是」關「,它將用空字符串替換警告。

這裏是我比較瞭解代碼:

public static void RunProgram(string file, string outdirectory, RichTextBox rtfReport, ToolStripComboBox debug, ToolStripComboBox warning, bool addNewLine) 
{ 
    Process procUnpackFile = new Process(); 
    ProcessStartInfo procStartInfo1 = new ProcessStartInfo(); 
    procStartInfo1.RedirectStandardOutput = true; 
    procStartInfo1.RedirectStandardError = true; 
    procStartInfo1.UseShellExecute = false; 
    procStartInfo1.CreateNoWindow = true; 
    AppendText(rtfReport, "Using (" + program + ") to extract (" + Path.GetFileName(file) + ")..." + Environment.NewLine, Color.Yellow, true); 
    AppendText(rtfReport, "TOOL.EXE [OUTPUT]", Color.Cyan, true); 
    AppendText(rtfReport, "======================================================================================================================", Color.Teal, true); 
    if (debug.Text.ToString() == "On") //If Statement for DebugComboBox 
    { 
     AppendText(rtfReport, "#################### DEBUG LOG ####################", Color.DarkMagenta, true); 
     AppendText(rtfReport, "Command Prompt Input: (" + program + " " + file + " " + outdirectory + ")", Color.Magenta, true); 
     AppendText(rtfReport, "###################################################", Color.DarkMagenta, true); 
    } 
    procStartInfo1.FileName = "cmd"; 
    procStartInfo1.WorkingDirectory = tooldir; 
    procStartInfo1.Arguments = "/c " + program + " " + file + " " + outdirectory; 
    procUnpackFile.StartInfo = procStartInfo1; 
    try 
    { 
     procUnpackFile.Start(); 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     if (!procUnpackFile.HasExited) 
     { 
      procUnpackFile.Kill(); 
     } 
    } 
    if (procUnpackFile.Responding) 
    { 
     string output = procUnpackFile.StandardOutput.ReadToEnd(); 
     string error = procUnpackFile.StandardError.ReadToEnd(); 
     procUnpackFile.WaitForExit(); 

     if (warning.Text.ToString() == "Off") //If Statement for WarningComboBox 
     { 
      for (int i = 0; i < 200000; i++) 
      { 
       if (error.Contains("0 [main] " + program + " " + i)) 
       { 
        error.Replace("  0 [main] " + program + " " + i + " find_fast_cwd: WARNING: Couldn't compute FAST_CWD Pointer. Please report this problem to the public mailing list [email protected]", ""); 
        AppendText(rtfReport, error, Color.Red, true); 
       } 
      } 
     } 
     else 
     { 
      AppendText(rtfReport, error, Color.Red, true); 
     } 
     AppendText(rtfReport, output, Color.White, true); 
     AppendText(rtfReport, "======================================================================================================================", Color.Teal, true); 
    } 
    else 
    { 
     if (!procUnpackFile.HasExited) 
     { 
      procUnpackFile.Kill(); 
     } 
    } 
} 
+0

你可以驗證使用斷點/控制檯登錄,當'debug.Text'爲 「開」 和'warning.Text 「是」關閉「它甚至通過了第二條if語句?然後,您可以從其[MSDN](https://msdn.microsoft.com/en-us/library/fk49wtc1(v = vs.110).aspx)重新檢查它是否存在「error.Replace」問題)它不會修改'error',如果你想改變錯誤,試試'error = error.Replace(「0 [ma ...'。 –

+0

錯誤是一個字符串,它有被替換的能力。在MSDN中,因爲當使用它時,函數的使用就像這樣[string.Replace(string-to-find,string-to-replace)],我剛剛完成了使用你的代碼,看看它是否會有所作爲,現在不管是什麼警告或錯誤都會在RichTextBox中打印5次,讓它以另一種方式替換字符串,但只有在DebugComboBox值爲「Off」時 – DeathlyPlague

+0

好吧,我不會試圖將代碼切換回正常狀態,無論我如何做錯誤打印5次 – DeathlyPlague

回答

0

感謝Keyur PATEL幫助我解決我的,如果對組合框

if(debug.SelectedItem.ToString() == "On") if(warning.SelectedItem.ToString() == "Off")

+0

選擇您的答案爲正確答案,以便人們知道已解決。很高興幫助:) –

+0

我必須等2天,如果你不介意,你可能會發布它?我會標記你的:P – DeathlyPlague