文件路径中的空格问题 - C# 中的命令行执行

发布于 2024-10-02 04:00:59 字数 1004 浏览 0 评论 0原文

我正在为命令行程序构建一个图形用户界面。 在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苍白女子 2024-10-09 04:00:59
proc.StartInfo.Arguments = text + " " + txtBoxUrls.Text + " " + txtFileName.Text; 

在此行中,text 已包含 txtBoxUrls 字符串的正确引用版本。为什么要以不带引号的形式再次添加它们(+ txtBoxUrls.Text)?如果我正确理解您的代码,则以下内容应该有效:

proc.StartInfo.Arguments = text + " " + txtFileName.Text;    

事实上,由于 txtFileName.Text 可能包含空格,您也应该引用它,只是为了确定:(

proc.StartInfo.Arguments = text + " \"" + txtFileName.Text + "\"";    

或者,使用您的语法: )

proc.StartInfo.Arguments = text + @" """ + txtFileName.Text + @"""";    
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 + @"""";    
卸妝后依然美 2024-10-09 04:00:59

通常,为了解决文件名中的空格,您需要将参数用双引号引起来。如果省略引号,程序会认为它有两个参数。像这样的东西......

wk.exe "C:\VS2008\Projects\web2pdf\web2pdf\bin\Release\Test Page.htm"

此外,这一行似乎有太多引号。四个,而不是三个:

s1 = @"""" + s + @"""";

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...

wk.exe "C:\VS2008\Projects\web2pdf\web2pdf\bin\Release\Test Page.htm"

Also, this line appears to have too many quotes. Four, instead of three:

s1 = @"""" + s + @"""";
甜宝宝 2024-10-09 04:00:59

看一下 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文