我使用cmd打开程序来解压缩文件。在我的ToolStripMenu中,我创建了一个WarningToggleComboBox和一个DebugToggleComboBox。
所以在另一个类中,我创建了一个运行cmd的函数,并在RichTextBox中获取错误和输出。
在错误之上我做了一个If语句,声明如果DebugToggleComboBox =" On"然后将特定文本附加到RichTextBox。
错误之后我做了一个If语句,声明如果WarningToggleComboBox =" Off"它将在包含随机整数的错误中搜索特定行,并用空字符串替换警告文本。
问题是每次我选择DebugToggleComboBox值为" On"然后我选择WarningToggleComboBox为" Off"它打印出警告5次,而不是用空字符串替换它,但是当DebugToggleComboBox值为" Off"而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 cygwin@cygwin.com", "");
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();
}
}
}