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

问题描述

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

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

我目前转义基本报价和反斜杠,但最近我的输入已经开始包含(全角引号)。

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:

包含空格或以空格开头或结尾
必须用双引号括起来。

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

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
switch是一个参数,它修改
命令的操作在一些
方式。有时,开关以
a +或 - 字符开头。一些开关是
全局的,并且影响命令
,而不管它们在
参数列表中的位置。其他开关是
local,并影响特定的参数
(例如开关旁边的
之前的开关)。

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字符传递为
命令的参数的一部分,
或者整个参数必须是用双引号括起来的
,否则
保留字符必须转义。

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

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解释
的保留字符必须
被绕过时,转义是必要的

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 b未记录的
    。简单的
    测试显示Windows NT
    命令shell允许非常长的
    命令 - 超过4,000
    个字符。实际上,
    对命令长度
    没有明显的上限。

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

10-30 13:27