命令行参数,带空格的路径,解析错误 [英] Command line arguments, paths with spaces, parsing incorrectly

查看:95
本文介绍了命令行参数,带空格的路径,解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题与此处概述的问题几乎相同(控制台应用程序无法正确解析带有空格的args ),但是该答案与我的情况无关。



我收到了我的简单程序的参数是路径和路径+文件名,其中可能包含很多空格。发生的情况是路径中的空格比我实际产生的参数更多。我通过传递args数组来手动重建所需的内容,但是今天我发现,如果文件名有两个空格,则我的方法将不起作用,并且在程序中无法解决该问题。


上传 \somesrv\Data\cust\99999_Test4\Working\ \somesrv\Data\cust\ 99999_Test4\Working\99999_Test4-GUAST-买家列表PA 11 15 14_112514_101753 AM_Sht =和stuff_Automation\99999_Test4-GUAST-买家列表PA 11 15 14_112514_101753 AM_Sht =买家的列表PA_11 11_14514_112514 >

因此,在那组参数的情况下,我得到了一个包含21个元素的args []。鉴于某一点上有一对空格,我不希望对此进行更正。



我有什么办法可以确保路径完好无损?在提供或接收端都很好,因为我可以同时控制两者,但不能控制路径/文件名本身。



代码:

 静态void Main(string [] args)
{
// args 0 =切换值
// args 1 =工作目录
//参数2 = Mail.dat zip文件

if(args.Length == 0)
{
Environment.Exit(-1) ;
}

字符串maildatName = args [1] .Substring(args [1] .IndexOf( Working)+ 8,args [1] .Length-(args [1]。 IndexOf(正在工作)+8))+;

for(int x = 2; x {
maildatName + = args [x];
maildatName + =;
}

args [1] = args [1] .Substring(0,args [1] .IndexOf( Working)+ 7);
maildatName = maildatName.Trim();
}


解决方案

是由 \ 在第一个参数的末尾。



如果您删除结尾的斜杠或在其之前添加一个额外的斜杠用引号引起来,您将得到两个参数。引号被转义,并被认为是第一个参数的一部分,直到它在两个字符后到达下一个引号为止。此后,您不再位于引号内,因此每个空格导致另一个引数添加到 args 数组中。



这是由于.Net使用 CommandLineToArgvW 处理命令行参数及其用于处理字符转义的规则。



Jon Galloway有一个在此

>

要引用:


命令LineToArgvW对反斜杠字符加引号字符()
的特殊解释如下:





  • (2n)+ 1个反斜杠再加上引号再产生n个反斜杠,再加上引号。

  • (2n)+ 1个反斜杠再加上引号。
  • li>
  • n个反斜杠(不带引号)只会产生n个反斜杠。



I have an issue that's nearly identical to the one outlined here (Console app not parsing correctly args with white spaces) but that answer has no bearing on my situation.

I'm receiving arguments to my simple program that are paths and path+filenames with potentially lots of spaces in them. What's happening is that spaces in the paths are generating many more arguments than I actually have. I was manually reconstructing what I needed by passing through the args array, but I discovered today that if the filename has two spaces my method won't work, and I can't know that in the program to fix it.

Upload "\somesrv\Data\cust\99999_Test4\Working\" "\somesrv\Data\cust\99999_Test4\Working\99999_Test4-GUAST - Buyers List PA 11 15 14_112514_101753 AM_Sht=And stuff_Automation\99999_Test4-GUAST - Buyers List PA 11 15 14_112514_101753 AM_Sht=And stuff_Automation.zip"

So in the case of that set of arguments I get an args[] with 21 elements. Given that there's a pair of spaces at one point I have no hope of correcting this.

Is there anything I can do to make sure that the paths come through in tact? On the giving or receiving end is fine, as I'm in control of both, but not the paths/filenames themselves.

Code:

static void Main(string[] args)
{
    //args 0 = Switch value
    //args 1 = Working Directory
    //args 2 = Mail.dat zip file

    if (args.Length == 0)
    {
        Environment.Exit(-1);
    }

    string maildatName = args[1].Substring(args[1].IndexOf("Working")+8, args[1].Length - (args[1].IndexOf("Working")+8)) + " ";

    for (int x = 2; x < args.Length; x++)
    {
        maildatName += args[x];
        maildatName += " ";
    }

    args[1] = args[1].Substring(0, args[1].IndexOf("Working") + 7);
    maildatName = maildatName.Trim();
}

解决方案

It is caused by the \" at the end of your first argument.

If you remove the trailing slash or add an extra slash before the quote then you will get two arguments. The quote is being escaped and is considered part of the first argument until it hits the next quote 2 characters later. After that you are no longer inside of a quote and thus each space results in another argument added to the args array.

This is caused by the fact that .Net uses CommandLineToArgvW to process command line arguments and the rules it uses to handle escaping of characters.

Jon Galloway has a writeup on this

To quote:

CommandLineToArgvW has a special interpretation of backslash characters when they are followed by a quotation mark character ("), as follows:

  • 2n backslashes followed by a quotation mark produce n backslashes followed by a quotation mark.
  • (2n) + 1 backslashes followed by a quotation mark again produce n backslashes followed by a quotation mark.
  • n backslashes not followed by a quotation mark simply produce n backslashes.

这篇关于命令行参数,带空格的路径,解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆