Command Line Arguments

Command Line  Arguments

VB .Net provides easy access to the comand-line arguments.

Your application would usually be started in one of two ways: – From a desktp icon – From a menu item

But it is also possible to start an application by dragging a  file from an Explorer window and dropping it on the desktop icon for the  application.  If this happens, then the filename of the file that was  dropped is passed to the application as a command line argument.   It  is also possible to insert a command line argument in the start command that  starts the application from a menu item or desktop icon.

Yet another option is to associate your application with a  filename extension.  If this is done, then when a file with that extension  is double-clicked, your application will start.  As in the case of dropping  a file on an icon, the name of the file is passed to the application in the  command-line arguments.

The command line arguments are provided as an array of type  String, parsed from the command line using the rules that Explorer applies to  commands. They are accessed as   My.Application.CommandLineArgs for the collection object, and   My.Application.CommandLineArgs(n) for the n’th command line argument.

This example demonstrates how the command line arguments can  be accessed to carry out some operation on the file, if any, that was passed in  the command line.   It will examine the command line and if there  appears to be a filename in the argument list, it will copy that file to the  user’s My Documents folder.  If there is no file (for instance, if the user  has simply double-clicked the desktop icon for the applciation) the user’s My  Document folder will be opened in an Explorer window.

The example also shows how to use the special folders  enumeration and the Path.Combine method for creating file paths – both features  that have alternatives, but which benefit considerably from using the ‘correct’  .Net form.

The form has no design-time controls.

Imports System.IO Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As  System.EventArgs) Handles MyBase.Load
   Me.Show()
   Dim DestinationPath As String =  Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
  If My.Application.CommandLineArgs.Count = 0 Then
     Process.Start(DestinationPath)
   Else
     Dim FilePath As String = My.Application.CommandLineArgs(0)
     Dim FileName As String = Path.GetFileName(FilePath)
     Dim Destination As String = Path.Combine(DestinationPath,  FileName)
     Try
       File.Copy(FilePath, Destination, True)
     Catch ex As Exception
       MsgBox(ex.ToString)
     End Try 
   End If
   Application.Exit()
 End Sub
 End Class
Advertisements
  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s