Results 1 to 8 of 8

Thread: App does not get CommandLine Args

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    220

    App does not get CommandLine Args

    When debugging my app it works fine i.e. It gets the two commandline args, through a loop, and execute as expected. However, once I publish it, the app only picks up the first argument, which is the .exe file string. What am I doing wrong here? Plz help. Code below:
    _____________________________________________________

    Public Sub Main()

    Dim args() As String = Environment.GetCommandLineArgs()

    Dim n As integer = args.Length
    Dim i as integer

    MsgBox("No of args is:" & n)

    While i < n

    _ _ _


    Next

    End Sub

    (passed args as: -a -k, in the File Debug Properties)

    n is 3 in Debug mode and 1 after publishing the app. Why?
    ____________________________________________

    Thanks for helping.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: App does not get CommandLine Args

    As the name would suggest, the Debug page of the project properties only applies to debugging. If you want to pass commandline arguments to your app after deploying it then you have to pass commandline apps to your app after deploying it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: App does not get CommandLine Args

    You need to actually pass the arguments to the executable. The easy way is via a Windows shortcut. The arguments passed in the debug property window is only for debugging mode.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    220

    Re: App does not get CommandLine Args

    Thanks for the reply. I think I did have a sense that these args remain within the debug scope. How does one pass the args to the executable. I am not clear about thhe Windows shortcut. Can you take me through the process plz. Thanks

  5. #5
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: App does not get CommandLine Args

    Quote Originally Posted by kumika View Post
    Thanks for the reply. I think I did have a sense that these args remain within the debug scope. How does one pass the args to the executable. I am not clear about thhe Windows shortcut. Can you take me through the process plz. Thanks
    Usually when we deploy an application a shortcut is created via an MSI created with Wise Installer. I am sure that Click Once creates shortcuts.

    If you want to create a shortcut in your project Windows Scripting can do this. The attached VS2008 project has a function with overloads to create shortcuts using Windows Scripting. Build the project then add it as a reference to your project then after adding the reference find the reference under Imported namespaces on the same tab that you added the reference.

    Now you can create a shortcut. The demo belows creates a shortcut for your application on the users desktop passing two arguments and assigns the apps icon to the shortcut.

    Code:
    Dim ShortcutName = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "MyApp.lnk")
    Dim IconFileName = System.Reflection.Assembly.GetExecutingAssembly.Location() & ", 0"
    CreateShortcut(Application.ExecutablePath, ShortcutName, "-A -B", IconFileName)
    Simple code to check the arguments sent in. Of course in your check you have logical statements to determine if the right args where sent.
    Code:
    Dim ArgCount As Integer = Environment.GetCommandLineArgs().Count
    If ArgCount > 1 Then
        Dim sb As New System.Text.StringBuilder
        For item As Integer = 1 To ArgCount - 1
            sb.AppendLine(Environment.GetCommandLineArgs(item))
        Next
        MessageBox.Show(sb.ToString)
    End If
    Practical example where you checked the argument count first
    Code:
    Dim Args() = Environment.GetCommandLineArgs()
    If Args.Contains("-A") AndAlso Args.Contains("-B") Then
        MessageBox.Show("Let um in")
    Else
        MessageBox.Show("Guards we have an intruder")
    End If
    Attached Files Attached Files

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    220

    Re: App does not get CommandLine Args

    Thanks again. I had no clue about that scriptting. Just two questions plz. Where would you slot in the shortcut script within the project code. Is it in the Sub Main, FormLoad, (top/bottom) or at the end of the application. Secondly I know that commandLine args can also be sourced through an array rather than the project properrties/Windows Shortcut. Is it a fair conclusion to say the Array option is more reliable, neat/short and advisable, since all you do is pass array elements to the Sub Main. Thanks

  7. #7
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    Re: App does not get CommandLine Args

    Quote Originally Posted by kumika View Post
    Thanks again. I had no clue about that scriptting. Just two questions plz. Where would you slot in the shortcut script within the project code. Is it in the Sub Main, FormLoad, (top/bottom) or at the end of the application. Secondly I know that commandLine args can also be sourced through an array rather than the project properrties/Windows Shortcut. Is it a fair conclusion to say the Array option is more reliable, neat/short and advisable, since all you do is pass array elements to the Sub Main. Thanks
    In regards to the scripting, not to sound condescending but of course you have no clue since you had not considered it as an option. I supplied the “how to create a Windows shortcut with arguments” for showing how to do this but would not be actually creating a shortcut this way but instead create a shortcut in your install program be it Click Once or another installation builder.

    The code to check for proper command line arguments being passed could be in Sub Main, the main form’s Load event or New constructor.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Nov 2010
    Posts
    220

    Re: App does not get CommandLine Args

    Is the shortcut code placed in the Sub Main/formLoad as well?.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width