none
how call tihs code vb.net RRS feed

  • Question

  •    Sub Main(ByVal cmdArgs() As String)
            MsgBox("The Main procedure is starting the application.")
            Dim returnValue As Integer = 0
            ' See if there are any arguments.  
            If cmdArgs.Length > 0 Then
                For argNum As Integer = 0 To UBound(cmdArgs, 1)
                    ' Insert code to examine cmdArgs(argNum) and take  
                    ' appropriate action based on its value.  
                Next argNum
            End If
            ' Insert call to appropriate starting place in your code.  
            MsgBox("The application is terminating.")
        End Sub
    how call tihs code in form vb.net
    Tuesday, January 23, 2018 11:36 PM

Answers

  • Hello,

    You can access command line arguments in MyApplication namespace.

    Open project properties, select Application tab, click the button labeled "View Application Events" which opens up a new class file.

    Replace the code in the new file with the code below which provides access to the command arguments.

    Imports Microsoft.VisualBasic.ApplicationServices
    
    Namespace My
        Partial Friend Class MyApplication
            ''' <summary>
            ''' Property to get command arguments
            ''' </summary>
            ''' <returns></returns>
            Public ReadOnly Property CommandLineArguments As String()
                Get
                    Return Environment.GetCommandLineArgs
                End Get
            End Property
            ''' <summary>
            ''' Used to get command argument count
            ''' </summary>
            ''' <returns></returns>
            Public ReadOnly Property HasCommandLineArguments As Boolean
                Get
                    Return CommandLineArguments.Length > 1
                End Get
            End Property
            ''' <summary>
            ''' Process them as you want.
            ''' </summary>
            ''' <param name="sender"></param>
            ''' <param name="e"></param>
            Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
                If HasCommandLineArguments Then
                    ' skip first as this is thyself
                    For indexer As Integer = 1 To CommandLineArgs.Count
                        Console.WriteLine(CommandLineArguments(indexer))
                    Next
                Else
                    Console.WriteLine("Nothing passed in via arguments")
                End If
            End Sub
    
        End Class
    End Namespace
    

    Now if you need access to the two properties above say in a form we can do something like the following.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If My.Application.HasCommandLineArguments Then
            For Each arg As String In My.Application.CommandLineArgs
                Console.WriteLine(arg)
            Next
        End If
    End Sub

    Test it by dropping some test arguments in and then run the code.

    Results in Visual Studio Output window


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 24, 2018 2:45 AM
    Moderator

All replies

  • Hi

    3 things.

    First: use the code block tool from the toolbox to post code. You should know this as you have been using this forum for a couple of years.

    Second: you can't just copy some code from somewhere and ask here how to use it.

    Third: have you any idea what it is doing, or what it is for, or, why you want it?


    Regards Les, Livingston, Scotland

    Wednesday, January 24, 2018 12:22 AM
  • Hi,

    how call tihs code in form vb.net

    Please write like this:
    Call Main(string array)

    [Note]
    (1) cmdArgs() is a parameter of Main.  And it is string array. 
        You need to define cmdArgs like "Dim cmdArgs As String()" and provide values to it before calling Main.
    (2) If Main is located in other class/module etc, add its name to Main as a prefix.
          e.g. Main is in class clsSomething, code would be "Call clsSomething.Main(string array)"

    Regards,

    Ashidacchi

    Wednesday, January 24, 2018 12:33 AM
  •  Just a guess that you are using a windows form application...  You would place the Sub Main in a Module that you add to the Form project.  Then you would need to un-check the "Enable application framework" in the application's properties.  Last you would set the "Startup Form" to "Sub Main".  You can follow the directions GIVEN HERE.  I also see that code you posted appears to be from Main Procedure in Visual Basic.

     The Sub Main will now be called when your application is being started.  You would not call this sub from your code,  it is only used for when your application is being started by the user in some way,  weather it be by double clicking the exe,  a shortcut,  or from a CMD prompt.


    If you say it can`t be done then i`ll try it

    • Edited by IronRazerz Wednesday, January 24, 2018 2:01 AM
    Wednesday, January 24, 2018 2:00 AM
  • Hello,

    You can access command line arguments in MyApplication namespace.

    Open project properties, select Application tab, click the button labeled "View Application Events" which opens up a new class file.

    Replace the code in the new file with the code below which provides access to the command arguments.

    Imports Microsoft.VisualBasic.ApplicationServices
    
    Namespace My
        Partial Friend Class MyApplication
            ''' <summary>
            ''' Property to get command arguments
            ''' </summary>
            ''' <returns></returns>
            Public ReadOnly Property CommandLineArguments As String()
                Get
                    Return Environment.GetCommandLineArgs
                End Get
            End Property
            ''' <summary>
            ''' Used to get command argument count
            ''' </summary>
            ''' <returns></returns>
            Public ReadOnly Property HasCommandLineArguments As Boolean
                Get
                    Return CommandLineArguments.Length > 1
                End Get
            End Property
            ''' <summary>
            ''' Process them as you want.
            ''' </summary>
            ''' <param name="sender"></param>
            ''' <param name="e"></param>
            Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
                If HasCommandLineArguments Then
                    ' skip first as this is thyself
                    For indexer As Integer = 1 To CommandLineArgs.Count
                        Console.WriteLine(CommandLineArguments(indexer))
                    Next
                Else
                    Console.WriteLine("Nothing passed in via arguments")
                End If
            End Sub
    
        End Class
    End Namespace
    

    Now if you need access to the two properties above say in a form we can do something like the following.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If My.Application.HasCommandLineArguments Then
            For Each arg As String In My.Application.CommandLineArgs
                Console.WriteLine(arg)
            Next
        End If
    End Sub

    Test it by dropping some test arguments in and then run the code.

    Results in Visual Studio Output window


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, January 24, 2018 2:45 AM
    Moderator