简体   繁体   English

在控制台应用程序中,防止转义命令行参数中特殊序列的最佳方法是什么?

[英]in a console app what's the best way to prevent special sequences in command line arguments from being escaped?

I wrote this console app that reads a sequence of command line arguments and does something with them. 我编写了这个控制台应用程序,它读取一系列命令行参数并对其进行处理。

The problem is that if the user enters something like: 问题是,如果用户输入如下内容:

 --folder "C:\my folder\" --username john

the args String array of the Main function will have 2 elements rather than 4: Main函数的args String数组将包含2个元素,而不是4个:

1st element: "--folder"
2nd element: "C:\my folder\" --username john"

(the \\" sequence is escaped as a double quote.) (\\“序列以双引号转义。)

Since not using quotes would result in 5 elements... 由于不使用引号将导致5个元素...

--folder C:\my folder\ --username john

1st element: --folder
2nd element: C:\my
3rd element: folder\
4th element: --username
5th element: john

... what's the best way to get around this problem? ...解决这个问题的最佳方法是什么?

This is a consequence of the weird command line parsing rules on Windows. 这是Windows上奇怪的命令行解析规则的结果。 See here http://msdn.microsoft.com/en-us/library/windows/desktop/17w5ykft(v=vs.85).aspx . 请参阅http://msdn.microsoft.com/zh-CN/library/windows/desktop/17w5ykft(v=vs.85).aspx The command line should actually be: --folder "C:\\my folder\\\\" --username john 命令行实际上应该是:-- --folder "C:\\my folder\\\\" --username john

This is a user input error. 这是用户输入错误。 You shouldn't try to correct it code; 您不应该尝试更正它的代码。 just fail gracefully. 只是优雅地失败。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用命令行参数自动化WinForms应用程序的最佳方法是什么? - What't the best way to automate a WinForms app with command line arguments? asp.net Core/OnPostAsync:防止记录被创建两次的最佳方法是什么? - asp.net Core/OnPostAsync: what's the best way to prevent a record from being created twice? 将一行“数据”从一个C#控制台应用程序传递到另一个C#控制台应用程序的最佳方法是什么? - What's the best way to pass a “row of data” from one C# console app to another C# console app? 防止用户在卸载时启动应用程序的最佳方法是什么 - What is the best way to prevent a user from starting an app while uninstall 防止类被实例化的最佳方法? - Best way to prevent a class from being Instantiated? .NET控制台应用程序命令行解析文件参数 - .NET Console app command line parsing of file arguments 在进程之间传递命令行参数的最佳方法 - Best way to pass command line arguments between processes 防止从对象传递未使用的属性的最佳方法 - Best way to prevent unused properties from being passed from object 从Web-> Web api->控制台应用程序进行通讯并再次返回的最佳方法是什么? - What is the best way of communicating from web->web api->console app and back again? 如何防止C#控制台应用程序中的Ctrl键生成特殊字符? - How do I prevent special characters from being generated by the Ctrl key in a C# console application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM