Browsing to a file to open

cpopham

Well-known member
Joined
Feb 18, 2004
Messages
273
Okay,
I am new to programming and especially vb.net. I have been programming in VBA for a little while though. This is what I would like to do.

I would like to have a button on a form that sys simply file. I know how to add this to a form etc. But this is what I am having a hard time trying to figure out. When you click the File button I would like something to popup so that you can navigate the particular text file on you hard drive. Perhaps a tree view to drill down through. I will be reading the file sequentially and then deleteing the first character from each line of the file. I would then like this changed file to overwrite the file that it was read from.

The reason i would liek to do this is because we have txt files from a mainframe that several people get and they can be anywhere on their harddrives. We have to manually delete the first character of each line as it is now. I would like to just have a simple app fo this.

I think I would like it to be along the lines of the FolderBrowser, but it only shows folders and not files. Anyone have any ideas?

Thanks for the help,
Chester
 
Last edited by a moderator:
take a look at the OpenFileDialog and the System.IO.StreamReader / StreamWriter classes , heres an example i knocked up for you ...
Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim oDialog As New OpenFileDialog()
        Dim sReader As IO.StreamReader /// to open the file for reading
        Dim sWriter As IO.StreamWriter /// to write to the file

        Dim strText As String

        With oDialog
            .InitialDirectory = "C:\"
            .Filter = "textfiles|*.txt"
            If .ShowDialog = DialogResult.OK Then
                sReader = New IO.StreamReader(New IO.FileStream(.FileName, IO.FileMode.Open))
                While Not sReader.Peek = -1
                    strText += sReader.ReadLine.Substring(1) & Environment.NewLine /// append each line of your files text Minus the first character to a string buffer.
                End While
                sReader.Close() /// close the opened file.

                sWriter = New IO.StreamWriter(.FileName, False) /// False will overwrite the existing file.
                sWriter.Write(strText)
                sWriter.Close()
            End If
        End With
    End Sub
 
That worked great. I put message box in to let the user know that it deleted the characters also! Now along that same concept what bit of code would you need to change to have it delete the first character on each line and then all blank spaces at the beginning of the line...
 
I am also getting an error if the very first character is a space. Could I put this line in:
strText += sReader.ReadLine.Substring(1) & Environment.NewLine /// append each line of your files text Minus the first character to a string buffer

Then add a trimstart to it somehow to get rid of the spaces? I have two types of things I have to do, one is delete the first character of each line in the text file. And the other is with somefiles, I have to delete that first character and then all blank spaces at the beginning of each line of text.

Chester
 
Cant you just send this part into the TrimStart method?
sReader.ReadLine.Substring(1).TrimStart(" ")
then & Environment.NewLine and &= to strText?
 
That works fine. Now one more thing. How can I take this file when I have completed deleting the characters and have it automatically open in NotePad?

Chester
 
The original file is opened with the open dialog so how could I get the new formtted file to just open in notepad? We have to do other modifications to this text file after deleting the initial characters or blank spaces. So i was hoping to get this file to launch into NotePad after the characters have been deleted.

This is how I have it coded so far:

Private Sub DeleteOneButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DeleteOneButton.Click
Dim oDialog As New OpenFileDialog
Dim sReader As IO.StreamReader /// to open the file for reading
Dim sWriter As IO.StreamWriter /// to write to the file

Dim strText As String

With oDialog
.InitialDirectory = "C:\"
.Filter = "textfiles|*.txt"
If .ShowDialog = DialogResult.OK Then
sReader = New IO.StreamReader(New IO.FileStream _
(.FileName, IO.FileMode.Open))
While Not sReader.Peek = -1
strText += sReader.ReadLine.Substring(1) & Environment.NewLine /// append each line of your files text Minus the first character to a string buffer.
End While
sReader.Close() /// close the opened file.
sWriter = New IO.StreamWriter(.FileName, False) /// False will overwrite the existing file.
sWriter.Write(strText)
MessageBox.Show("First character delete successful!", "Text", MessageBoxButtons.OK, MessageBoxIcon.Information)
sWriter.Close()
End If
End With

End Sub

Private Sub ExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ExitButton.Click
Me.Close()

End Sub

Private Sub BlankDeleteButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BlankDeleteButton.Click
Dim oDialog As New OpenFileDialog
Dim sReader As IO.StreamReader /// to open the file for reading
Dim sWriter As IO.StreamWriter /// to write to the file

Dim strText As String

With oDialog
.InitialDirectory = "C:\"
.Filter = "textfiles|*.txt"
If .ShowDialog = DialogResult.OK Then
sReader = New IO.StreamReader(New IO.FileStream _
(.FileName, IO.FileMode.Open))
While Not sReader.Peek = -1
strText += sReader.ReadLine.Substring(1).TrimStart(" ") & Environment.NewLine /// append each line of your files text Minus the first character to a string buffer.
End While
sReader.Close() /// close the opened file.
sWriter = New IO.StreamWriter(.FileName, False) /// False will overwrite the existing file.
sWriter.Write(strText)
MessageBox.Show("Initial blanks ", "Text", MessageBoxButtons.OK, MessageBoxIcon.Information)
sWriter.Close()
End If
End With
End Sub

Chester
 
Seems like you just need to replace strPath in sysops example with the oDialog.Filename... since filename is the name of the file, thats what youll want to send to Process.Start and thats the file that you want to open in Notepad. :)
 
Okay great.. That is what I need. I put that in my if statement and then stuck a Me.Close() in there to close the app.
 
Back
Top