Improve the way that I'm passing debug commandline arguments

后端 未结 1 600
走了就别回头了
走了就别回头了 2021-01-16 03:41

Im my console application I pass the arguments like this:

#Region \" DEBUG CommandLine Arguments \"

    Private Function Set_CommandLine_Arguments() As List         


        
相关标签:
1条回答
  • 2021-01-16 04:16

    I think it might be the way you are trying to manage the debug command line.

    If you go to Project Properties -> Debug, under start options you can set a starting command line. If you also escape file/path names with quotes (like Windows does in the registry and for shortcut paths), the arguments/path will remain intact. For instance:

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

    There are multiple ways to get the command line arguments, but NET will parse the commandline for you. Two that will work anywhere are:

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

    The first will return the full commandline including the name of the current app as the first segment. GetCommandLineArgs will parse the commandline into a string array, again with args(0) being the name of the current executable.

    vbArgs = My.Application.CommandLineArgs
    

    This is slightly different, with just the arguments returned (no exe name) and the return is a ReadOnlyCollection(of String)

    There is still another way, reminiscent of the bygone days of DOS, which is very useful:

    Public Sub Main(args As String())
    

    Rather than start the app from a main form, in Properties -> Application, set it the start up object as Sub Main, then create a sub in a module as above. If you add a string array parameter, NET will parse the commandline, skip the executable name and pass you the arguments in the array. I personally prefer this because I rarely care about the executable name.

    This may be particularly useful in this case because if there is a commandline, you probably do not want to show a Form or start a message pump. Even with a straight WinForm app, the start up order of things can be different or include extra steps when passed a file to open or whatever.

    You can get the same set of args in VS/VB by setting the starting commandline as described at the outset. All of the GetCommandArgs variations will return the same info in the IDE as at runtime (with the exception that those which include the EXE name, which will report ...myApp.vshost.exe in the IDE).

    Example

    Using this commandline in VS project properties:

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

    (Line breaks surely distort things here)

    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
    

    Output:

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

    Spaces are preserved in the second arg(1), because they were escaped with quotes as the user will have to do - the quotes are also removed. Copy them to a list for processing or whatever the app needs to do.

    You get the same args the same way from the same place whether it is runtime or in the IDE with nothing to do differently.

    0 讨论(0)
提交回复
热议问题