Link to home
Start Free TrialLog in
Avatar of fastburst
fastburst

asked on

vb.net open file with exe created.

Ok, this is something I havent had to do before and I know there is a way.
Example:
You go to my computer and see a text file and you RIGHT-CLICK it and Select OPEN WITH here you get an option to open with notepad wordpad etc. OR you can browse to a different application to open the file with.

Here is where I want the application to be able to open the file with the Application I created. However when I select my Application, it launches the application but doesnt load anything into the textbox iteself.

I know I have to place some code to handle that, the question is what code and how?
Below is the simple textreader app I would like to know where and what code would handle this. Searching the Internet I found 1000's of things that look just like my code but havent found anything that would do what I described.
Imports System.IO
Public Class Form1

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim oReader As StreamReader
        OpenFileDialog1.CheckFileExists = True
        OpenFileDialog1.CheckPathExists = True
        OpenFileDialog1.DefaultExt = "txt"
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        OpenFileDialog1.Multiselect = False
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            oReader = New StreamReader(OpenFileDialog1.FileName, True)
            TextBox1.Text = oReader.ReadToEnd
        End If
    End Sub
End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The filename will be passed to your application as a command line parameter.  This can be obtained in either the Application.Startup() event, as Sub Main() arguments, or in other locations using Environment.GetCommandLineArgs():
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim args() As String = Environment.GetCommandLineArgs
        ' Index 0 (zero) is the application name itself
        ' Index 1 should be the passed in filename:
        If args.GetUpperBound(0) >= 1 Then
            Try
                If System.IO.File.Exists(args(1)) Then
                    TextBox1.Text = System.IO.File.ReadAllText(args(1))
                End If
            Catch ex As Exception
                MessageBox.Show("File: " & args(1) & vbCrLf & vbCrLf & ex.ToString, "Error Opening File", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub

End Class

Open in new window

Guess I'm a little slow today!...   =D
Avatar of fastburst
fastburst

ASKER

LOL Sorry Idle_Mind, your usually the first to reply to mine. Maybe next time. =)