改进我传递调试命令行参数的方式

我是我的控制台应用程序我传递这样的参数:

#Region " DEBUG CommandLine Arguments " Private Function Set_CommandLine_Arguments() As List(Of String) #If DEBUG Then ' Debug Commandline arguments for this application: Dim DebugArguments = "HotkeyMaker.exe /Hotkey=Escape /run=notepad.exe" Return DebugArguments.Split(" ").ToList #Else ' Nomal Commandline arguments: Return My.Application.CommandLineArgs.ToList #End If End Function #End Region 

但这有一个很明显的问题,空格字符会产生误报,例如:

 MyProgram.exe /Run="Process path with spaces.exe" 

我们都知道,通常参数是用括号双引号" "字符或space字符分隔的标记分隔的,所以我会得到很多误报来定制我的调试参数。

C#VBNET如何才能改进函数以获取正确分隔的(自定义)参数列表?

更新2:

我做了这个例子来试图澄清我的意图:

 Module Module1 '''  ''' Debug commandline arguments for testing. '''  Private ReadOnly DebugArguments As String = "ThisProcess.exe /Switch1=Value /Switch2=""C:\folder with spaces\file.txt""" '''  ''' Here will be stored the commandline arguments of this application. ''' If DebugArguments variable is nothing then the "normal" arguments which are passed directly from the console are used here, ''' Otherwise, my custom debug arguments are used. '''  Private Arguments As List(Of String) = Set_CommandLine_Arguments() Sub Main() Parse_Arguments() End Sub Private Sub Parse_Arguments() For Each Arg As String In Arguments MsgBox(Arg) ' Result: ' ------ ' 1st arg: ThisProcess.exe ' 2nd arg: /Switch1=Value ' 3rd arg: /Switch2="C:\folder ' 4th arg: with ' 5th arg: spaces\file.txt" Next Arg ' Expected arguments: ' ------------------ ' 1st arg: ThisProcess.exe ' 2nd arg: /Switch1=Value ' 3rd arg: /Switch2="C:\folder with spaces\file.txt" End Sub Public Function Set_CommandLine_Arguments() As List(Of String) #If DEBUG Then If Not String.IsNullOrEmpty(DebugArguments) Then ' Retun the custom arguments. Return DebugArguments.Split.ToList Else ' Return the normal commandline arguments passed directly from the console. Return My.Application.CommandLineArgs.ToList End If #Else ' Return the normal commandline arguments passed directly from the console. Return My.Application.CommandLineArgs.ToList #End If End Function End Module 

我认为这可能是您尝试管理调试命令行的方式。

如果转到“项目属性” – >“调试”,则可以在启动选项下设置启动命令行。 如果您还使用引号转义文件/路径名(如注册表中的Windows和快捷方式路径),则参数/路径将保持不变。 例如:

/Hotkey=Escape /run="\Program Files\folder name\notepad.exe"

有多种方法可以获取命令行参数,但.NET会为您解析命令行。 两个可以在任何地方工作的是:

 myCmd = Environment.CommandLine args = Environment.GetCommandLineArgs() 

第一个将返回完整的命令行,包括当前应用程序的名称作为第一个段。 GetCommandLineArgs将命令行解析为字符串数组,同样args(0)是当前可执行文件的名称。

 vbArgs = My.Application.CommandLineArgs 

这只是略有不同,只返回了参数(没有exe名称),返回的是ReadOnlyCollection(of String)

还有另一种方式,让人联想到DOS过去的日子,这非常有用:

 Public Sub Main(args As String()) 

而不是从主窗体启动应用程序,在属性 – >应用程序中,将启动对象设置为Sub Main ,然后在上面的模块中创建一个sub。 如果添加字符串数组参数,NET将解析命令行,跳过可执行文件名并传递数组中的参数。 我个人更喜欢这个,因为我很少关心可执行文件的名称。

在这种情况下,这可能特别有用,因为如果有命令行,您可能不希望显示表单或启动消息泵。 即使使用直接的WinForm应用程序,启动顺序的东西也可能不同,或者在传递文件打开时包含额外的步骤。

您可以通过如开头所述设置启动命令行在VS / VB中获取相同的args集。 所有GetCommandArgs变体都将在IDE中返回与运行时相同的信息(除了那些包含EXE名称的信息,它将在IDE中报告...myApp.vshost.exe )。

在VS项目属性中使用此命令行:

/Hotkey=Escape /run="\Program Files\folder name\notepad.exe" "/arg=this is an arg"

(换行肯定会扭曲事情)

 Public Sub Main(args As String()) For j As Integer = 0 To args.Length - 1 Console.WriteLine("arg {0} == {1}", j, args(j)) Next End Sub 

输出:

arg 0 == / Hotkey = Escape
arg 1 == / run = \ Program Files \ folder name \ notepad.exe
arg 2 == / arg =这是一个arg

空格保留在第二个arg(1)中,因为它们是用引号转义的,因为用户必须这样做 – 引号也被删除了。 将它们复制到列表中进行处理或应用程序需要执行的任何操作。

无论是运行时还是在IDE中,您都可以从同一个地方以相同的方式获得相同的args,而无需做任何不同的操作。