When debugging my app it works fine i.e. It gets the two commandline args, through a loop, and execute as expected. However, once I publish it, the app only picks up the first argument, which is the .exe file string. What am I doing wrong here? Plz help. Code below:
_____________________________________________________
Public Sub Main()
Dim args() As String = Environment.GetCommandLineArgs()
Dim n As integer = args.Length
Dim i as integer
MsgBox("No of args is:" & n)
While i < n
_ _ _
Next
End Sub
(passed args as: -a -k, in the File Debug Properties)
n is 3 in Debug mode and 1 after publishing the app. Why?
____________________________________________
As the name would suggest, the Debug page of the project properties only applies to debugging. If you want to pass commandline arguments to your app after deploying it then you have to pass commandline apps to your app after deploying it.
You need to actually pass the arguments to the executable. The easy way is via a Windows shortcut. The arguments passed in the debug property window is only for debugging mode.
Thanks for the reply. I think I did have a sense that these args remain within the debug scope. How does one pass the args to the executable. I am not clear about thhe Windows shortcut. Can you take me through the process plz. Thanks
Thanks for the reply. I think I did have a sense that these args remain within the debug scope. How does one pass the args to the executable. I am not clear about thhe Windows shortcut. Can you take me through the process plz. Thanks
Usually when we deploy an application a shortcut is created via an MSI created with Wise Installer. I am sure that Click Once creates shortcuts.
If you want to create a shortcut in your project Windows Scripting can do this. The attached VS2008 project has a function with overloads to create shortcuts using Windows Scripting. Build the project then add it as a reference to your project then after adding the reference find the reference under Imported namespaces on the same tab that you added the reference.
Now you can create a shortcut. The demo belows creates a shortcut for your application on the users desktop passing two arguments and assigns the apps icon to the shortcut.
Code:
Dim ShortcutName = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "MyApp.lnk")
Dim IconFileName = System.Reflection.Assembly.GetExecutingAssembly.Location() & ", 0"
CreateShortcut(Application.ExecutablePath, ShortcutName, "-A -B", IconFileName)
Simple code to check the arguments sent in. Of course in your check you have logical statements to determine if the right args where sent.
Code:
Dim ArgCount As Integer = Environment.GetCommandLineArgs().Count
If ArgCount > 1 Then
Dim sb As New System.Text.StringBuilder
For item As Integer = 1 To ArgCount - 1
sb.AppendLine(Environment.GetCommandLineArgs(item))
Next
MessageBox.Show(sb.ToString)
End If
Practical example where you checked the argument count first
Code:
Dim Args() = Environment.GetCommandLineArgs()
If Args.Contains("-A") AndAlso Args.Contains("-B") Then
MessageBox.Show("Let um in")
Else
MessageBox.Show("Guards we have an intruder")
End If
Thanks again. I had no clue about that scriptting. Just two questions plz. Where would you slot in the shortcut script within the project code. Is it in the Sub Main, FormLoad, (top/bottom) or at the end of the application. Secondly I know that commandLine args can also be sourced through an array rather than the project properrties/Windows Shortcut. Is it a fair conclusion to say the Array option is more reliable, neat/short and advisable, since all you do is pass array elements to the Sub Main. Thanks
Thanks again. I had no clue about that scriptting. Just two questions plz. Where would you slot in the shortcut script within the project code. Is it in the Sub Main, FormLoad, (top/bottom) or at the end of the application. Secondly I know that commandLine args can also be sourced through an array rather than the project properrties/Windows Shortcut. Is it a fair conclusion to say the Array option is more reliable, neat/short and advisable, since all you do is pass array elements to the Sub Main. Thanks
In regards to the scripting, not to sound condescending but of course you have no clue since you had not considered it as an option. I supplied the “how to create a Windows shortcut with arguments” for showing how to do this but would not be actually creating a shortcut this way but instead create a shortcut in your install program be it Click Once or another installation builder.
The code to check for proper command line arguments being passed could be in Sub Main, the main form’s Load event or New constructor.