C#命令行解析引用路径和避免转义字符 [英] C# Command-Line Parsing of Quoted Paths and Avoiding Escape Characters

查看:549
本文介绍了C#命令行解析引用路径和避免转义字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解析要解释为路径的命令行参数? args []包含自动连接的字符串,如果它们被引用,例如:

How is it possible to parse command-line arguments that are to be interpreted as paths? args[] contains strings that are automatically joined if they are quoted, e.g.:

example.exe一两三四

example.exe one two "three four"

args[0] = one
args[1] = two
args[2] = three four

但是,args []不会将C:\Example \解析为参数。相反,它将提供参数作为C:\Example(包含额外的引号)。这是由于路径中的反斜杠被视为转义字符,因此用户提供的结束引号命令行成为参数的一部分。

However, args[] will not property parse "C:\Example\" as an argument. Rather, it will supply the argument as "C:\Example"" (with the extra quote included.) This is due to the backslash in the path being treated as an escape character and thus the end quotation that the user supplied on the command-line becomes part of the argument.

.eg:

example.exe一个C: \\InputFolderC:\OutuptFolder \

example.exe one "C:\InputFolder" "C:\OutuptFolder\"

args[0] = one
args[1] = C:\InputFolder"
args[2] = C:\OutputFolder"

一个简单的污点可能是:

An easy kludge might be:

_path = args[i].Replace("\"", @"\");

但是,我相信有一个最佳实践。如何正确解析包含路径的命令行,防止args []数组被不正确地填充已经为转义字符解析的stings?

However, I'm sure there is a best-practice for this. How might one correctly parse a command line that inlcudes paths, preventing the args[] array from improperly being populated with stings that have been parsed for escape characters?

注意:I不想在我的项目中包括一个完整的命令行解析库!我只需要处理引用的路径,并希望以手动方式这样做。请不要推荐NConsoler,Mono或任何其他大型厨房水槽命令行解析库。

NOTE: I would not like to include an entire command-line parsing library in my project! I need only to handle quoted paths and wish to do so in a "manual" fashion. Please do not reccomend NConsoler, Mono, or any other large "kitchen sink" command-line parsing library.

另请注意:据我所知,这不是一个重复的问题。虽然其他问题集中在通用命令行解析,这个问题是特定于路径引入时,它们的部分被解释为转义序列的问题。

ALSO NOTE: As far as I can tell, this is not a duplicate question. While other questions focus on generic command-line parsing, this question is specific to the problem that paths introduce when parts of them are interpreted as escape sequences.

推荐答案

不是答案,但这里有一些背景和解释来自Jeffrey Tan,Microsoft在线社区支持(12/7/2006):

Not an answer, but here's some background and explanation from Jeffrey Tan, Microsoft Online Community Support (12/7/2006):


注意:这不是代码失败
但是通过设计,因为backslashe是
通常用于转义某个
特殊字符。此外,此
算法与Win32命令相同
线参数解析函数
CommandLineToArgvW。请参阅下面的注释
部分:
http:// msdn2.microsoft.com/en-us/library/bb776391.aspx

也参考FX方法< a href =http://msdn.microsoft.com/en-us/library/system.environment.getcommandlineargs.aspx =nofollow noreferrer> Environment.GetCommandLineArgs ,以进一步说明斜线处理行为。

Also makes reference to the FX method Environment.GetCommandLineArgs for further explanation of the slash handling behavior.

我个人认为这是一个阻力,我很惊讶,我以前没有一点点。或者也许我有而且不知道吗?盲目的用斜杠替换引号并不能解决我的问题。我投票的问题,因为它是一个开眼界。

Personally I think this is a drag, and I'm surprised I haven't been bit by it before. Or maybe I have and don't know it? Blind replacement of quotes with slashes doesn't strike me as a solution, though. I'm voting the question up, because it was an eye opener.

这篇关于C#命令行解析引用路径和避免转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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