为Process.Start转义字符串 [英] Escape string for Process.Start

查看:181
本文介绍了为Process.Start转义字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何逃避一个未知的字符串作为参数传递给Process.Start?

How can I escape an unknown string for passing to Process.Start as an argument?

我目前正在逃避基本的引号和反斜杠,但最近我的输入已经开始包含如 http://www.fileformat.info/info/unicode /char/ff02/index.htm (全宽引号)。

I currently escape basic quotes and backslashes, but recently my input has started to contain things like http://www.fileformat.info/info/unicode/char/ff02/index.htm (Fullwidth quotation mark).

所以我的问题是,我需要逃避什么安全地传递一个字符串作为Process.Start的参数?

So my question is, what all do I need to escape to safely pass a string as an argument for Process.Start?

编辑:
所以我需要澄清一下。我正在寻找的是所有字符的列表,必须用cmd.exe引用的字符串(foo)进行转义。我原来处理了双引号字符和反斜杠字符,但是我终于有一些输入,其中包含一个全引号(如上所述),这也需要被转义。所以问题是,还有什么我需要逃避引用的字符串参数传递给cmd.exe与Process.Start?

So I need to clarify this. What I really am looking for is a list of all characters that have to be escaped in a quoted string ("foo") for cmd.exe. I originally dealt with double quote character as well as backslash character, but I finally had some input that contained a fullwidth quotation mark (as referenced above) which also needed to be escaped. So the question is, what else do I need to escape for a quoted string argument passed to cmd.exe with Process.Start?

推荐答案

p>这可能是有用的:

This might be useful:



  • 首先,多个参数通常是相互分离的
    空格。在图2.3中,命令有
    三个参数c:*。bak,e:\backup,
    和/ s。偶尔使用其他字符
    作为参数分隔符。对于
    示例,COPY命令可以使用+
    个字符来分隔多个
    文件名。

  • First, multiple arguments are normally separated from one another by spaces. In Figure 2.3, the command has three arguments, c:*.bak, e:\backup, and /s. Occasionally, other characters are used as argument separators. For example, the COPY command can use + characters to separate multiple filenames.

其次,包含空格或以空格开始或结尾
必须用双引号括起来。

使用长的文件和目录名称
时,这通常包含一个或多个
空格,这一点尤为重要。如果双引号
本身包含一个双引号
字符,则双引号必须为
倍。例如,输入引用
参数为引用参数。

Second, any argument that contains spaces or begins or ends with spaces must be enclosed in double quotes. This is particularly important when using long file and directory names, which frequently contain one or more spaces. If a double-quoted argument itself contains a double quote character, the double quote must be doubled. For example, enter "Quoted" Argument as """Quoted"" Argument".

第三,命令开关始终以斜杠/字符。 A
开关是以
方式修改
操作命令的参数。偶尔,交换机以
a +或 - 字符开始。一些开关是
全局,并影响命令
,无论他们在
参数列表中的位置。其他交换机是
本地,并影响特定参数
(例如紧接在
之前的交换机)。

Third, command switches always begin with a slash / character. A switch is an argument that modifies the operation of the command in some way. Occasionally, switches begin with a + or - character. Some switches are global, and affect the command regardless of their position in the argument list. Other switches are local, and affect specific arguments (such as the one immediately preceding the switch).

第四,所有保留的shell字符不在双引号中必须
被转义。这些字符对于Windows NT
命令shell具有
的特殊含义。保留的shell
字符是:

Fourth, all reserved shell characters not in double quotes must be escaped. These characters have special meaning to the Windows NT command shell. The reserved shell characters are:

& | () > ^

& | ( ) < > ^

要将命名的参数的保留shell字符作为
传递给
整个参数必须是
,用双引号括起来,否则
保留字符必须被转义。

克拉(^)字符前缀保留字符以将其转义。对于
示例,以下命令示例
将无法正常工作,因为<
和>是保留的shell字符:

To pass reserved shell characters as part of an argument for a command, either the entire argument must be enclosed in double quotes, or the reserved character must be escaped. Prefix a reserved character with a carat (^) character to escape it. For example, the following command example will not work as expected, because < and > are reserved shell characters:

  1. C:\>echo <dir>
  2. The syntax of the command is incorrect.

  Instead, escape the two reserved characters, as follows:

  1. C:\>echo ^<dir^>
  2. <dir>

通常,保留的shell
字符不会在命令中使用,
需要使用
转义的冲突是罕见的。他们确实发生,但
。例如,流行的
PKZIP程序支持 - &切换到
启用磁盘跨越。要在Windows NT下正确使用此
开关, - ^&
必须输入。

Typically, the reserved shell characters are not used in commands, so collisions that require the use of escapes are rare. They do occur, however. For example, the popular PKZIP program supports a -& switch to enable disk spanning. To use this switch correctly under Windows NT, -^& must be typed.

提示:克拉字符本身是一个保留的shell字符。因此,对于
,将一个克拉字符作为
命令参数的一部分,键入两个克拉
。逃避只需
,当正常的shell解释
的保留字符必须绕过

Tip: The carat character is itself a reserved shell character. Thus, to type a carat character as part of a command argument, type two carats instead. Escaping is necessary only when the normal shell interpretation of reserved characters must be bypassed.


  • 最后,
    a shell命令的最大允许长度似乎是
    ,由Microsoft无证。简单的
    测试显示Windows NT
    命令shell允许非常长的
    命令 - 超过4,000
    个字符。实际上,
    在命令的长度上没有明确的
    的上限。

http://technet.microsoft.com/en-us/图书馆/ cc723564.aspx

这篇关于为Process.Start转义字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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