我正在为命令行程序构建一个 gui。在 txtBoxUrls[TextBox] 文件路径中逐行输入。如果文件路径包含空格,则程序无法正常工作。该程序如下所示。
string[] urls = txtBoxUrls.Text.ToString().Split(new char[] { '\n', '\r' });
string s1;
string text;
foreach (string s in urls)
{
if (s.Contains(" "))
{
s1 = @"""" + s + @"""";
text += s1 + " ";
}
else
{
text += s + " ";
}
}
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.FileName = @"wk.exe";
proc.StartInfo.Arguments = text + " " + txtFileName.Text;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
//Get program output
string strOutput = proc.StandardOutput.ReadToEnd();
//Wait for process to finish
proc.WaitForExit();
例如txtBoxUrls中输入的文件路径为“C:\VS2008\Projects\web2pdf\web2pdf\bin\Release\Test Page.htm”,程序将无法运行。这个带双引号的文件路径可以很好地在 Windows 命令行中工作(我没有使用 GUI)。有什么解决办法。