文件路径中的空格问题 - C# 中的命令行执行
我正在为命令行程序构建一个图形用户界面。 在 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)。 解决办法是什么。
I am building a gui for a commandline program.
In txtBoxUrls[TextBox] file paths are entered line by line.
If the file path contains spaces the program is not working properly.
The program is given below.
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();
For example if the file path entered in txtBoxUrls is "C:\VS2008\Projects\web2pdf\web2pdf\bin\Release\Test Page.htm", The program will not work. This file path with double quotes will work in the windows command line(I am not using the GUI) nicely.
What would be the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在此行中,
text
已包含 txtBoxUrls 字符串的正确引用版本。为什么要以不带引号的形式再次添加它们(+ txtBoxUrls.Text
)?如果我正确理解您的代码,则以下内容应该有效:事实上,由于
txtFileName.Text
可能包含空格,您也应该引用它,只是为了确定:(或者,使用您的语法: )
In this line,
text
already contains the properly quoted version of your txtBoxUrls strings. Why do you add them again in unquoted form (+ txtBoxUrls.Text
)? If I understood your code corrently, the following should work:In fact, since
txtFileName.Text
could probably contain spaces, you should quote it as well, just to be sure:(or, using your syntax:)
通常,为了解决文件名中的空格,您需要将参数用双引号引起来。如果省略引号,程序会认为它有两个参数。像这样的东西......
此外,这一行似乎有太多引号。四个,而不是三个:
Usually to get around spaces in filenames, you'll need to wrap your argument in double quotes. If you leave out the quotes the program will think it has two arguments. Something like this...
Also, this line appears to have too many quotes. Four, instead of three:
看一下 Path 类 - http://msdn.microsoft .com/en-us/library/system.io.path.aspx
Path.combine 可能就是您正在寻找的内容。
Take a look at the Path class - http://msdn.microsoft.com/en-us/library/system.io.path.aspx
Path.combine might be what you are looking for.