本文介绍了如果文件夹包含空格,如何处理文件路径中的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  public static void launchProcess(string processName,string arguments,out string output)
{
Process p = new Process
{
StartInfo = {UseShellExecute = false,RedirectStandardOutput = true,FileName = processName,Arguments = arguments}
};

p.Start();
output = p.StandardOutput.ReadToEnd();
p.WaitForExit();





$ b $ p如果我的参数包含像$ b $这样的文件名b D:\ Visual Studio项目\ProjectOnTFS\ProjectOnTFS
然后我得到错误

也许你需要在引号中包含你的文件夹参数。

例如

pre $ code > string myArgument =D:\ Visual Studio Projects\ProjectOnTFS\ProjectOnTFS;

  // NB额外的单引号
string myArgument ='D:\ Visual Studio Projects\ProjectOnTFS\ProjectOnTFS';

或者其他方式

  string myArgument ='D:\ Visual Studio Projects\ProjectOnTFS\ProjectOnTFS'; 

编辑,感谢Almo



实际上我的回答在上面。

它需要双引号,但也可能需要@来处理字符串单词()即\在字符串中有特殊含义eg \t表示一个标签,所以我们要忽略\



所以不仅是双引号,而且还有@

  string myArgument = @D:\ Visual Studio Projects\ProjectOnTFS\ProjectOnTFS; 


public static void launchProcess(string processName, string arguments, out string output)
{
    Process p = new Process
    {
        StartInfo = { UseShellExecute = false, RedirectStandardOutput = true, FileName = processName, Arguments = arguments }
    };

    p.Start();
    output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

}

and if my arguments contains the file names likeD:\Visual Studio Projects\ProjectOnTFS\ProjectOnTFSthen i get the error

解决方案

I think perhaps you need to contain your folder argument in quotes.

e.g

string myArgument = "D:\Visual Studio Projects\ProjectOnTFS\ProjectOnTFS";

to

//n.b. additional single quotes ' '
string myArgument = "'D:\Visual Studio Projects\ProjectOnTFS\ProjectOnTFS'";

or maybe the other way around

string myArgument = '"D:\Visual Studio Projects\ProjectOnTFS\ProjectOnTFS"';

EDIT, thanks Almo

Actually scrap my answer above.

It'll need doubles quotes, but will also likely need an @ to treat the string word-for-word (verbatim string) i.e. the "\" has a special meaning in string e.g. \t means a tab, so we want to ignore the \

So not only the double quotes, but also @

string myArgument = @"D:\Visual Studio Projects\ProjectOnTFS\ProjectOnTFS";

这篇关于如果文件夹包含空格,如何处理文件路径中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 13:39