问题在文件路径的空间 - 在C#中的命令行执行 [英] Problem with spaces in a filepath - commandline execution in C#
问题描述
我建立了一个命令行程序的GUI。
在txtBoxUrls [文本框]文件路径是通过线进入线。
如果文件路径包含的空格程序无法正常工作。
程序如下。
的String [] =的URL txtBoxUrls.Text.ToString()。斯普利特(新的char [] {'\\\
','\r'});
串S1;
字符串文本;
的foreach(字符串s在URL中)
{
如果(s.Contains())
{
S1 = @+ S + @ ;
文字+ = S1 +;
}
,否则
{
+文字= S +;
}
}
的System.Diagnostics.Process PROC =新的System.Diagnostics.Process();
proc.StartInfo.CreateNoWindow = TRUE;
proc.StartInfo.FileName = @wk.exe
proc.StartInfo.Arguments =文字++ txtFileName.Text;
proc.StartInfo.UseShellExecute = FALSE;
proc.StartInfo.RedirectStandardOutput = TRUE;
proc.Start();
//获取程序的输出
串strOutput = proc.StandardOutput.ReadToEnd();
//等待进程结束
proc.WaitForExit();
例如,如果在txtBoxUrls输入的文件路径为C:\VS2008\Projects\ web2pdf\web2pdf\bin\Release\Test Page.htm,该程序将无法正常工作。用双引号此文件路径将在Windows命令行的工作(我不使用GUI)很好。
,会是什么解决办法。
proc.StartInfo.Arguments =文字++ txtBoxUrls.Text ++ txtFileName.Text;
在这一行,文本
已经包含了正确引用您的txtBoxUrls字符串版本。为什么你在不带引号的形式重新添加它们( + txtBoxUrls.Text
)?如果我理解corrently你的代码,下面应该工作:
proc.StartInfo.Arguments =文字++ txtFileName.Text ;
在事实上,自从 txtFileName.Text
能可能包含空格,你应该引用它为好,只是可以肯定的:
proc.StartInfo.Arguments =文字+\\ \\+ txtFileName.Text +\;
(或者使用你的语法:)
proc.StartInfo.Arguments =文字+ @+ txtFileName.Text + @;
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.
proc.StartInfo.Arguments = text + " " + 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:
proc.StartInfo.Arguments = text + " " + txtFileName.Text;
In fact, since txtFileName.Text
could probably contain spaces, you should quote it as well, just to be sure:
proc.StartInfo.Arguments = text + " \"" + txtFileName.Text + "\"";
(or, using your syntax:)
proc.StartInfo.Arguments = text + @" """ + txtFileName.Text + @"""";
这篇关于问题在文件路径的空间 - 在C#中的命令行执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!