Articles

Solved How do I pass parameters from DOS to the VB.NET application

February 17, 2013 at 19:37:43
Specs: Windows 7, 2

How do I pass parameters from DOS to the VB.NET application ? I've tried but nothnig gets displayed in the VB.NET app when I try to interrogate Command() stored in a string.

like start Add.application 1,2 etc......

Nothing seems to get passed to the VB App

Thanks
Dave
18 Feb 2013


See More: How do I pass parameters from DOS to the VB.NET application

Report •


✔ Best Answer
February 18, 2013 at 15:53:06

I guess that means I will need to have two applications.
You don't. I prefer to do sample code as a console application, because its code fits in a single file. You can easily convert it to a WinForms application. The underlying logic does not change.
Option Infer On : Option Strict On
Public Class Form1
  Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) _
  Handles Me.Shown
    Dim args = My.Application.CommandLineArgs.ToArray()
    If args.Length = 0 Then
      Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location, _
             "arg1 arg2 arg3 arg4")
      Close()
    End If
    MessageBox.Show(Join(args, vbNewLine))
  End Sub
End Class


EDIT: If it doesn't work, it's a problem on your end. I've tested it, and it works.

Mon 2013.02.18 19.33.32++
M:\bin\Debug>type \Form1.vb
Option Infer On : Option Strict On
Public Class Form1
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Shown
        Dim args = My.Application.CommandLineArgs.ToArray()
        If args.Length = 0 Then
            Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location, _
                          "arg1 arg2 arg3 arg4")
            Close()
        Else
            MessageBox.Show(Join(args, vbNewLine))
        End If
    End Sub
End Class


Mon 2013.02.18 19.33.46++
M:\bin\Debug>WindowsApplication1 123 456 789
Produces:
123
456
789

How To Ask Questions The Smart Way



#1
February 17, 2013 at 20:10:38

I haven't encountered problems with this using "old" visbasic. You might post your code (simplified toward the issue at hand). Not sure about .net...
Attribute vb_name = "test"
sub main
x=command
msgbox x
end sub
delivers the goods using my version.

Report •

#2
February 17, 2013 at 20:36:55

Hmm. I'm using VB.NET 2008 I got the code of YouTube : http://www.youtube.com/watch?v=r7V2...

However it was written for a VB.NET to VB.NET parameter transfer. I thought it might work by calling the VB.NET App from DOS along with passing command line arguments from DOS, however no argument data seems to get displayed in the VB app.

Thus the VB app starts ok, just that no command line parameters / argument data get displayed after it starts.

APP CODE BELOW

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

''MsgBox(Command())
MsgBox(Command)

Try
Dim ReceivedText As String = Command()
'MsgBox(ReceivedText)
Me.Label1.Text = ReceivedText
Me.Label1.Refresh()

Catch ex As Exception
MsgBox("Error: " & (ex.Message))
End Try
End Sub


Report •

#3
February 17, 2013 at 21:51:38

mebbe try it without the parens ():
x=command
msgbox x

I'm quite lame when it comes to .net.


Report •

Related Solutions

#4
February 17, 2013 at 22:22:25

Thanks,

VB.NET puts () on it, even if you type it without, so Command() seems to be correct. Just not picking up the command arguments when calling the VB app from DOS.


Report •

#5
February 18, 2013 at 07:48:17

Consider the following (command line) application:
Option Infer On : Option Strict On
Module Module1
  Sub Main()
    Dim args = My.Application.CommandLineArgs.ToArray()
    If args.Length = 0 Then
      Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location, _
                    "arg1 arg2 arg3 arg4")
      Exit Sub
    End If
    Console.WriteLine(Join(args, vbNewLine))
    Console.Write("Press any key to exit . . . ")
    Console.ReadKey(True)
  End Sub
End Module

How To Ask Questions The Smart Way


Report •

#6
February 18, 2013 at 15:33:42

Thanks, but what does this app do ? Doesn't seem to work for me in VB.NET 2008

i.e. the program crashes.

"Says cannot read keys when either application does not have a console or when console input has been redirected from a file. Try Console Read."

Ahh I had to make a console application to get it to work. Seems to work ok now. Never used a VB console app before. I guess that means I will need to have two applications.

1. A console application to be called from DOS with arguments

2. My regular VB.NET application that I presume this console app will then load with the same arguments.

Does that sound right ?

Seems a bit convolouted to have to have this extra appliaiton, but if it works then it works I guess.

All that said, it's still not displaying the argument data when called from DOS with arguments. Instead it's just displaying the variable names

arg1
arg2
arg3
arg4



Report •

#7
February 18, 2013 at 15:53:06
✔ Best Answer

I guess that means I will need to have two applications.
You don't. I prefer to do sample code as a console application, because its code fits in a single file. You can easily convert it to a WinForms application. The underlying logic does not change.
Option Infer On : Option Strict On
Public Class Form1
  Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) _
  Handles Me.Shown
    Dim args = My.Application.CommandLineArgs.ToArray()
    If args.Length = 0 Then
      Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location, _
             "arg1 arg2 arg3 arg4")
      Close()
    End If
    MessageBox.Show(Join(args, vbNewLine))
  End Sub
End Class


EDIT: If it doesn't work, it's a problem on your end. I've tested it, and it works.

Mon 2013.02.18 19.33.32++
M:\bin\Debug>type \Form1.vb
Option Infer On : Option Strict On
Public Class Form1
    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles Me.Shown
        Dim args = My.Application.CommandLineArgs.ToArray()
        If args.Length = 0 Then
            Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location, _
                          "arg1 arg2 arg3 arg4")
            Close()
        Else
            MessageBox.Show(Join(args, vbNewLine))
        End If
    End Sub
End Class


Mon 2013.02.18 19.33.46++
M:\bin\Debug>WindowsApplication1 123 456 789
Produces:
123
456
789

How To Ask Questions The Smart Way


Report •

#8
February 18, 2013 at 16:49:23

OK thanks. I've tried both versions and although they run without error, they don't seem to display the argument data though. i.e. if I run it from DOS with arguments.

For example DOS command prompt C:> MyApp.application zzz xxx ccc vvv

The application starts up ok, however just displays the argument variable names and not the actual data.

Any idea why ?

What format should I use when passing the arguments from DOS ?


Report •

#9
February 18, 2013 at 17:00:36

Well, it could be a few things, but I'd have to see exactly what you're typing. Try copy/pasting your command line and we'll run through the usual suspects. Also, it's not DOS. DOS is dead. It died 13 - 18 years ago. WinNT has a Command Prompt. CMD is also a valid choice.

How To Ask Questions The Smart Way


Report •

#10
February 18, 2013 at 17:08:58

Yep sorry from CMD prompt. Under Windows 7

C:\TEST>Add2.application zzz,mxx,xxx,ccc

Also tried without commas

C:\TEST>Add2.application zzz mxx xxx ccc

The output I get from both is the same. i.e. as follows;

arg1
arg2
arg3
arg4


Report •

#11
February 18, 2013 at 17:44:37

Don't invoke the .application file. Invoke the .exe file. Giving up command line arguments is one of the trade offs of using ClickOnce.

How To Ask Questions The Smart Way


Report •

#12
February 18, 2013 at 18:39:35

Ahh I see. OK it works fine. Thanks for that advice. I think I need to do a VB.NET course. Anyway I found the .exe version and it worked ok. Your help is much appreciated.

Report •


Ask Question