Debu
Debu

Reputation: 49

How to open a text file with 'open with' option by the software , created by visual basic?

In Windows we can open a text file by right clicking on a it, and selecting 'open with' option by any software like notepad, notepad++ etc. I want to create a notepad type software using visual basic , Which can open (load) any text file using 'open with' option in Windows explorer by right clicking. I tried by creating a simple notepad using textbox and button which can load text file by browsing file system and selecting the file . But not load by 'open with' option.

Upvotes: 0

Views: 2024

Answers (1)

Vector
Vector

Reputation: 3235

Here is an example. You need to understand that on my Windows 7 system the Default Program Setting for file type .txt is Sublime Text. I assume you understand that if you have .txt file type set to use Notepad. With my code on your system the text in the file will be displayed with Notepad using this line of code below.

Process.Start(openPath)

To use this code create a RichTextBox on your form a good size is about 1150 by 850 Name it rtbViewCode
You will need two Buttons named btnViewFile and btnCopy
I have included some bonus code that lets you copy the files text to the clipboard

 Private Sub btnViewFile_Click(sender As Object, e As EventArgs) Handles btnViewFile.Click
    Dim openFileDialog As OpenFileDialog = New OpenFileDialog()
    Dim dr As DialogResult = openFileDialog.ShowDialog()

    If dr = System.Windows.Forms.DialogResult.OK Then

        Dim openPath As String = openFileDialog.FileName
        MsgBox("openPath " & openPath)

        Process.Start(openPath)
        'Line of code above should be used behind it's own Button Click Event

        rtbViewCode.Text = File.ReadAllText(openPath)

    End If
End Sub

Private Sub btnCopy_Click(sender As Object, e As EventArgs) Handles btnCopy.Click
    rtbViewCode.SelectAll()
    rtbViewCode.Copy()
End Sub

Upvotes: 0

Related Questions