|
-
Jan 26th, 2009, 09:38 PM
#1
Thread Starter
Junior Member
How do I send a parameter to a Console Application
I created a Console Application, with a Sub Main(Byval str As String), and when I run the debugger I get the following error:
Error 1 No accessible 'Main' method with an appropriate signature was found in ...
Also I'd like the solution to my problem to apply not only to the debugger, but also to the .exe file that will eventually be created.
Also, I found the following code on the internet, but I don't understand how to send the parameter in still.
Code:
Public Sub Main()
MsgBox Command$,vbOk, "Command Line params"
End Sub
Do I have to open cmd.exe, type: programname.exe -parameter 'Or something of that sort?
Thanks
Last edited by 5te4lthX; Jan 26th, 2009 at 09:43 PM.
-
Jan 26th, 2009, 09:44 PM
#2
Re: How do I send a parameter to a Console Application
Your code should look like this:
vb.net Code:
Module Module1 Sub Main() For Each arg As String In Environment.GetCommandLineArgs() Console.WriteLine(arg) Next Console.ReadLine() End Sub End Module
or this:
vb.net Code:
Module Module1 Sub Main(ByVal args As String()) For Each arg As String In args Console.WriteLine(arg) Next Console.ReadLine() End Sub End Module
If your Main method does have a parameter it MUST be a String array. You can call it what you like but the convention is to name it "args".
Note that Environment.GetCommandLineArgs will return the application file name/path itself as the first argument, while the second method will not.
-
Jan 26th, 2009, 09:57 PM
#3
Thread Starter
Junior Member
Re: How do I send a parameter to a Console Application
After the Console.ReadLine(), the args didn't seem to be initialized. I checked it with my Debugger, and when I hovered over args, it said "length = 0".
When the console prompted me to type something, I typed: Test
Also, I was wondering if there is a way to start my program and send it a parameter at the same time, not send a parameter during runtime. Is this possible?
-
Jan 26th, 2009, 10:47 PM
#4
Re: How do I send a parameter to a Console Application
The only place you can send commandline parameters to an application is on the commandline, i.e. when you execute it.
Once your app is deployed then the user might execute it by selecting Start -> Run and typing in a commandline like "YourApp -someArg" (without quotes) or else create a desktop shortcut and add the argument to the commandline in the shortcut properties.
If you want to test your app with commandline arguments while debugging then you have to tell the IDE to pass them when it runs your app in the debugger. To do that you open the Debug page of the project properties and populate the appropriate field.
-
Mar 29th, 2009, 12:53 AM
#5
Thread Starter
Junior Member
Re: How do I send a parameter to a Console Application
 Originally Posted by jmcilhinney
vb.net Code:
Module Module1
Sub Main(ByVal args As String())
For Each arg As String In args
Console.WriteLine(arg)
Next
Console.ReadLine()
End Sub
End Module
I'm planning on using this. I'm wondering: how can there be more element in the array of args? In cmd, if I type application.exe command1 command2, does that mean that args contains two elements: {"command1", "command2"}? Or does it mean that it contains one element: {"command1 command2"}?
-
Mar 29th, 2009, 01:32 AM
#6
Re: How do I send a parameter to a Console Application
 Originally Posted by 5te4lthX
I'm planning on using this. I'm wondering: how can there be more element in the array of args? In cmd, if I type application.exe command1 command2, does that mean that args contains two elements: {"command1", "command2"}? Or does it mean that it contains one element: {"command1 command2"}?
Could you not just try it and see?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|