Passing Args of the file name that was double clicked on

redbrad0

Member
Joined
May 31, 2007
Messages
14
Programming Experience
Beginner
I have a VB.NET desktop application that when I double click on a file associated with the application I want the path of the file name sent to the application so I can load the file. I know I have to do something like the code below, but my application does not have a "Main" sub and im not sure how to add it.

VB.NET:
Public Sub Main(ByVal Args() As String)
If Args.GetLength(0) = 1 Then
Dim sFileToOpen as String = Args(0)
End if
End Sub

I do have the following two subs but I cant seem to throw in the Args into the sub

VB.NET:
    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub


VB.NET:
    Private Sub frmbidi_Load()

        Dim pd As New PrintDialog()         'printer dialog object

        ReadConfig()

        If ConnectionType = "usb" Then
            PrinterTxt.Text = USBPrinterName   'Display currently selected printer driver

        Else

        End If

    End Sub
 
VB.NET:
        Dim i As Integer = 0
        For Each StrArgument As String In Environment.GetCommandLineArgs()
            MsgBox("Argument " & i & ": " & StrArgument)
            i += 1
        Next

If I execute this app with the following

C:\AppToRun.exe C:\DocToOpen.abc

It will produce two message boxes

Argument 0: C:\AppToRun.exe
Argument 1: C:\DocToOpen.abc


This GetCommandLineArgs returns the app executable as the first argument.

Does this work for you?

Rob
 
I added the following code and yes it does tell me the file name BUT it also tells me the exe name of the file I am loading..

Message: Argument 0:c:\_tmp_files\bidi_vbnet\bidinet\bin\bidi.exe
Message: Argument 1:c:\_tmp_files\bidi_vbnet\bidinet\bin\testing.brad

I only want it to return the file types that are associated with this extension. Any way to do that?

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

ReadConfig()

Dim i As Integer = 0
For Each StrArgument As String In Environment.GetCommandLineArgs()
MsgBox("Argument " & i & ": " & StrArgument)
i += 1
Next
MessageBox.Show("Loaded")

'Add any initialization after the InitializeComponent() call

End Sub
 
VB.NET:
        Dim CmdArgs() As String = Environment.GetCommandLineArgs
        If CmdArgs.Length > 1 Then
            For i As Integer = 1 To CmdArgs.Length - 1
                MsgBox("Argument " & i & ": " & CmdArgs(i))
            Next
        End If

The above code will display only the command line arguments without the Executable. I am not certain if that is what you are looking for. If not could you elaborate on what you would like.

Thanks,

Rob
 
Ok I got it working with the below code, but the only problem is if the application closes because it sent information to the printer I get a windows debug message. It appears that its because it is still trying to load the form even though I am using Me.Hide and Me.Close. What else can I do to make sure it stops and doesnt load anymore?

VB.NET:
    Public Sub New()
        ReadConfig()

        Dim intTicketsPrinted As Integer
        Dim CmdArgs() As String = Environment.GetCommandLineArgs

        intTicketsPrinted = 0
        If CmdArgs.Length > 1 Then
            Dim strContents As String
            Dim objReader As StreamReader
            For i As Integer = 1 To CmdArgs.Length - 1
                Try
                    objReader = New StreamReader(CmdArgs(i))
                    strContents = objReader.ReadToEnd()
                    objReader.Close()

                    '# Send the data to the printer
                    If strContents.Trim() <> "" Then
                        PrintTickets(strContents)
                        intTicketsPrinted = intTicketsPrinted + 1
                    End If
                Catch ex As Exception
                    MessageBox.Show("There was a problem with " & CmdArgs(i) & vbNewLine & ESC.ToString())
                End Try
            Next
        End If

        If intTicketsPrinted > 0 Then
            Try
                Me.Hide()
                Me.Close()
            Catch ex As Exception
                MessageBox.Show("There was a error trying to close the application" & vbNewLine & ESC.ToString())
            End Try
        End If

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        If ConnectionType = "USB" Then
            PrinterTxt.Text = USBPrinterName   'Display currently selected printer driver
        Else
            PrinterTxt.Text = "** Ethernet **"
        End If


        'Add any initialization after the InitializeComponent() call

    End Sub
 
Ok, so if I understand you correctly then you are having errors closing the form because you are trying to close the form when you are initializing the form. I don't think you want all this code in this sub routine. You should consider moving the code into the Form_Load event. This should allow you to close the form without error (I believe). Another option is to create a module and put the Sub Main in it and do you work in that routine. If you need to exit all you'd have to do is exit that sub routine. You can show a form by using Form.ShowDialog()
 
Don't forget that the file that is associated with your app, must be set up so that it is opened with a command line of:

c:\path\to\your\app\YourApp.exe %1


Windows will suyb the name of the file you clicked on, into the %1
If you dont have that, then GetCommandLineArgs will return an empty array
 
but the only problem is if the application closes because it sent information to the printer I get a windows debug message.

VB.NET:
      Public Sub New()
        [COLOR="Red"][SIZE="1"]ReadConfig()

        Dim intTicketsPrinted As Integer
        Dim CmdArgs() As String = Environment.GetCommandLineArgs

        intTicketsPrinted = 0
        If CmdArgs.Length > 1 Then
            Dim strContents As String
            Dim objReader As StreamReader
            For i As Integer = 1 To CmdArgs.Length - 1
                Try
                    objReader = New StreamReader(CmdArgs(i))
                    strContents = objReader.ReadToEnd()
                    objReader.Close()

                    '# Send the data to the printer
                    If strContents.Trim() <> "" Then
                        PrintTickets(strContents)
                        intTicketsPrinted = intTicketsPrinted + 1
                    End If
                Catch ex As Exception
                    MessageBox.Show("There was a problem with " & CmdArgs(i) & vbNewLine & ESC.ToString())
                End Try
            Next
        End If

        If intTicketsPrinted > 0 Then
            Try
                Me.Hide()
                Me.Close()
            Catch ex As Exception
                MessageBox.Show("There was a error trying to close the application" & vbNewLine & ESC.ToString())
            End Try
        End If[/SIZE][/COLOR]
        'This call is required by the Windows Form Designer.
        InitializeComponent()

[COLOR="SeaGreen"][SIZE="1"]        If ConnectionType = "USB" Then
            PrinterTxt.Text = USBPrinterName   'Display currently selected printer driver
        Else
            PrinterTxt.Text = "** Ethernet **"
        End If[/SIZE][/COLOR]


        [B]'Add any initialization after the InitializeComponent() call[/B]

    End Sub


Just a small, quick question; given that the Windows Forms Designer hints to you to that you should put your own code after InitializeCOmponent.. howcome there's a huge block of code before it?


If youre looking for some action to happen without ever loading the main form, you should:

Disable the application framework
Pick a Shared Sub Main() to form the entry point to the app, NOT a form
Work out if you want to show the main window or not
If you do, call System.Windows.Forms.Application.Run(New MainForm())
If you do not, then do all your code without making a new form, or calling Application.Run
 
Back
Top