Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hey peeps, does anybody know how to retrieve the parameters passed to the compiled exe at runtime? In Java you get an array of strings right in front of you (args[]), so how is this done in VB.NET?

 

Thanks

Chaos is merely logic beyond human comprehension
Posted

Just add an array of strings as the parameter of the Main method.

Sub Main([b]ByVal args() As String[/b])
   ....
End Sub

"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted
Just add an array of strings as the parameter of the Main method.

Sub Main([b]ByVal args() As String[/b])
   ....
End Sub

 

...That does seem a little too easy...

 

*runs back to check*

 

By the 'Main' sub, are you referring to form_load? There are already two parameters in there by default:

 

(ByVal sender As System.Object, ByVal e As System.EventArgs)

Chaos is merely logic beyond human comprehension
Posted

In VB, you don't need a special "Main" method in a windows app (I think it generates one behind the scenes).

 

So you can either add one to your form:

Public Shared Sub Main(ByVal args() As String)

   ' Starts the app with whatever form you want:
   Application.Run(New Form1())

End Sub

or you can use Environment.CommandLine.

 

PS: Just in case this wasn't clear, the form_Load() is an event which is called when the form loads, and it is not the "Main" subroutine, therefore you cannot add the parameter args().

"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken
Posted

The way to do it is using Environment.GetCommandLineArgs

 

Environment.GetCommandLineArgs.Length is the count of parameters present

Environment.GetCommandLineArgs(n) is the nth comand line arg

 

That is all you need

Hamlet
Posted

Cheers guys, You've both been a great help.

 

Additionally:

 

Blackstone, The Main method you're referring to - does it run on startup like the one in Java, or does it need to be called?

Chaos is merely logic beyond human comprehension
Posted
It runs on startup (in a Windows App, you have to change the startup object to "sub Main()" )
"For every complex problem, there is a solution that is simple, neat, and wrong." - H. L. Mencken

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...