Tech Technology Tech Technology - 5 months ago 9
Vb.net Question

Set form's name equal dragged and dropped file title vb

I want to create a text editor in VB. My text editor can already read and write text files, but I want something better than that. When I drag and drop a .txt file on my .exe program, my program reads the context of the .txt file.
But the problem is that I want the [Form1.Text] to be the path of the file I have dropped. For example, when you open a file with Wordpad, you can see the name of the file on the top: TextFile.txt - Wordpad

And that's the code:

Private Sub ReadingText(sender As Object, e As EventArgs) Handles Me.Load
Dim arguments As String = Command()
If arguments = String.Empty Then
RichTextBox1.Text += vbCrLf & vbCrLf & vbCrLf
Else
Dim tempstr As String = arguments.Replace("""", "")
Dim SR As New System.IO.StreamReader(tempstr)
RichTextBox1.Text = vbCrLf & SR.ReadToEnd
Try
SR.Close()
Catch ex As Exception
RichTextBox1.Text += ex.ToString
End Try
End If
End Sub


Could you help??

Answer

You've already got the filename in your tempstr variable, just use it:

    Dim tempstr As String = arguments.Replace("""", "")
    Me.Text = tempstr & " - YourTextEditorNameHere"    
    Dim SR As New System.IO.StreamReader(tempstr)

If you don't want the full path, use Path.GetFileName():

    Dim tempstr As String = arguments.Replace("""", "")
    Me.Text = System.IO.Path.GetFileName(tempstr) & " - YourTextEditorNameHere"    
    Dim SR As New System.IO.StreamReader(tempstr)

And if you don't want the extension, use Path.GetFileNameWithoutExtension():

    Dim tempstr As String = arguments.Replace("""", "")
    Me.Text = System.IO.Path.GetFileNameWithoutExtension(tempstr) & " - YourTextEditorNameHere"    
    Dim SR As New System.IO.StreamReader(tempstr)
Comments