简体   繁体   中英

VB.Net code to run in command prompt

I have a done a VB.Net program that reads the data from two text files ( one text file will contain the list of server names and other will have the list of values) and hit the database with the server name from one text file's list and use the values in other text file as filters.

The code works fine in Dot Net environment.

The requirement is, I need to make it run in the command prompt.Input should be the following

MyProgram.exe -s D:\\ServerName.txt -v D:\\Valuelist.txt

MyProgram is the name of the program in VB.Net and the other two are text files.

Help me reaching out with this

Thanks, Ramya

To run from the command prompt as you described means that you need to create a Console Application . Selecting this project type when creating a new project means that your program will not have an interface, but instead run on the console.

The command-line parameters will be passed in to the Main event.

You could also use a normal Windows Application, and examine System.Environment.CommandLine to see the command-line that was used to start your application.

To test your applciation, specify the command-line attributes you'd like to run with in Project -> Properties -> Debug -> Command line arguments.

In the project's properties, under Application , set:
Application type = Console Application
Startup object = Sub Main

Next add a Module to your project (or use an existing one) and create a method similar to:

Sub Main(args() As String)
    If args.Length >= 2 Then
        ProcessFiles(args(0), args(1))
    Else
        AskForFiles()
    End If
End Sub

This method will be run when the application is executed. The args parameter will contain all command line arguments.


Alternatively, you can use a Windows Forms Application, in this case, when it launches, you need to check for command line arguments.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Form1.Load
    Dim args As String() = Environment.GetCommandLineArgs()
    If args.Length >= 2 Then
        ProcessFiles(args(0), args(1))
    Else
        AskForFiles()
    End If
End Sub

From your comments it looks like you're actually just running the command from Start Menu > Run (or its variations depending upon which version of Windows you're using).

This means a console window is created for your command to execute in and then it closes.

As a minimum you need to include the argument processing as described in the other answers.

To be able to continue to see the results of your program, either open the Command Prompt first yourself, or add a Console.ReadLine at the end of your program. Then it will wait for return to be pressed, or you can just close the window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM