Visual Basic > Visual Basic Forums > Visual Basic General > write / read from a text file
Ask a questionAsk a question
 

Answerwrite / read from a text file

  • Wednesday, November 04, 2009 8:45 PMAlvin Kuiper Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi
    I have a problem read / write from a text file
    When i read from the text file using
    Using st As StreamReader = New StreamReader("musik.txt")
    And when i write to the text file using:
    My.Computer.FileSystem.WriteAllText("musik.txt", OpenFileDialog1.FileName, True)
    Is it not the same place i read and write to , so how can I besure to read/write to this text file, the text file shall bee in my application.
    ALK

Answers

  • Thursday, November 05, 2009 10:40 PMFrank L. Smith Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    I'm not in front of a PC at the moment.
    If Alvin wants to use Open/Save FileDialogs, then show how to pre-select the Application's folder.
    Mark the best replies as answers. "Fooling computers since 1971."

    Rudy,

    Good point.

    @Alvin ->

    Try something like this:

    Dim baseFolder As String = Application.StartupPath
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            ' Button to open the text file
    
            OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt"
            OpenFileDialog1.FilterIndex = 1
            OpenFileDialog1.FileName = baseFolder & "\*.txt"
    
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                ' -----------------------
                ' Start the rest here ...
                ' -----------------------
            End If
    
    
        End Sub
    
    • Marked As Answer byAlvin Kuiper Friday, November 06, 2009 9:19 AM
    •  

All Replies

  • Wednesday, November 04, 2009 8:49 PMShariqDON Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
  • Wednesday, November 04, 2009 9:46 PMjinzai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    I do not see that you have actually read the file, and also...your write does not reference anything to write, except a filename string from your OpenDialog, which surely is not your intent. Here is a snippet both reads a file, and appends to the same file:

            ' Array to hold strings from the file
            Dim alltext() As String
            ' This is an easier way to get all of the strings in the file.
            alltext = File.ReadAllLines("musik.txt")
            ' This will append the string to the end of the file.
            My.Computer.FileSystem.WriteAllText("musik.txt", _
                "This is a literal string, or a string variable." & vbCrLf, _
                                                True)
    
    
  • Thursday, November 05, 2009 12:52 PMAlvin Kuiper Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi
    I can see that I have explain my self bad.
    I try again
    I have this:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim alltext() As String
            alltext = File.ReadAllLines("musik.txt")
           
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim text As String
    
            OpenFileDialog1.ShowDialog()
            text = OpenFileDialog1.FileName
            My.Computer.FileSystem.WriteAllText("musik.txt", _
            text, True)
    
        End Sub
    When i read the file musik.txt  (button1) I read from my application/bin/debug/musik.txt

    When i Write (button2) I write to a file musik.txt there comes in the folder where Openfiledialog have found the file

    So if i in openfiledialog select a file in c:\tut\myfile.doc
    then I write in a txt fil musik.txt in the folder c:\tut

    And what i want is to write in the same file i read from  /bin/debug/musik.txt

    ALK
  • Thursday, November 05, 2009 4:30 PMJohnWein Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Is it not the same place i read and write to , so how can I besure to read/write to this text file, the text file shall bee in my application.
    You new post doesn't explain your problem.  The file should be in the same location (your debug folder) when you read and write it.  Your code doesn't show what you write to the file or if you do anything with what you read from the file.

    I take that back.  Make "musik.txt", Application.StartupPath + "\musik.txt"
  • Thursday, November 05, 2009 4:36 PMRudedog2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Stop using the OpenFileDialog.

    It will read/write to the current output folder by default, without a path name on the file at all.


    Mark the best replies as answers. "Fooling computers since 1971."
  • Thursday, November 05, 2009 4:48 PMjinzai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Perhaps you should keep the full path to the file when you first use it...Have a look at this:

            Dim fi As New FileInfo("musik.txt")
    
            MsgBox(fi.FullName, MsgBoxStyle.Information)
    
    
    • Edited byjinzai Thursday, November 05, 2009 7:00 PMtypos
    •  
  • Thursday, November 05, 2009 6:49 PMCoolKing Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Well i will give u a very simple coding just replace the names. to view the file u need a multi line text box. then drag and drop 2 buttons name one as btnview and second as btnsave. btnview to view the file and btnsave to save the file. without selecting the file from the dialog box u can open and read the file directly. and wen u will save it will save directly. Now on the top of yr coding screen above the public class add this code.
    (Imports system.I.O.)
    dnt put in brakets. and then double click on btnview. and put this coding.


    Dim myReader As IO.StreamReader
    myReader = IO.File.OpenText("musik.txt")
    txtbox1.Text = myReader.ReadToEnd()
    myReader.Close()

    The codes above will read yr file and now to save it. In the text box u have viewed the file u can edit it in the same text box. means delete or write a few lines into the text box... Now the coding for btnsave.

    Dim mywriter As IO.StreamWriter
    Dim myreader As IO.StreamReader
    mywriter = IO.File.CreateText("musik.txt")
    mywriter.WriteLine(txtbox1.Text)
     mywriter.Flush()
    mywriter.Close()

    myreader = IO.File.OpenText("musik.txt")
     txtbox1.Text = myReader.ReadToEnd()
    myrecordreader.Close()


    The codes above will create a new music text file then it will write all the txt in the text box 1 and then it will replace it with the ancient one. and then will display the new one...
  • Thursday, November 05, 2009 8:00 PMFrank L. Smith Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi
    I can see that I have explain my self bad.
    I try again
    I have this:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim alltext() As String
    
            alltext = File.ReadAllLines("musik.txt")
    
           
    
    
    
        End Sub
    
    
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    
            Dim text As String
    
    
    
            OpenFileDialog1.ShowDialog()
    
            text = OpenFileDialog1.FileName
    
            My.Computer.FileSystem.WriteAllText("musik.txt", _
    
            text, True)
    
    
    
        End Sub
    When i read the file musik.txt  (button1) I read from my application/bin/debug/musik.txt

    When i Write (button2) I write to a file musik.txt there comes in the folder where Openfiledialog have found the file

    So if i in openfiledialog select a file in c:\tut\myfile.doc
    then I write in a txt fil musik.txt in the folder c:\tut

    And what i want is to write in the same file i read from  /bin/debug/musik.txt

    ALK

    If you want to be SURE that you're always looking to the same file to read from and then write to, create a variable that holds that and always reference that variable in the rest of your methods. For example: Dim fileToProcess as String = ....

    I'm not following why you're using a OpenFileDialog if you know in advance what file you want to work with. Maybe explain this a bit more?

    After that, there are a number of ways to go about reading/writing to the file, but I think your issue right now is using the file that you want. Try setting a variable like I showed above, then reference that and I think you'll be on the right track.

    Good luck!

    :)
  • Thursday, November 05, 2009 9:40 PMAlvin Kuiper Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi
    I shall use the path from openfiledialog, so i can read the file in my project

    So a user (me) shall select a file, this file path a want to write in a text file
    then later in the program i want to read from the txt fil and see the path to the file
    so I can use the file. In this case the file is a MP3 file.

    But i have to use the path, sp thats whay i use openfildialog.


    So instead write in my code "musik.txt cant i write something whit apllicationpath ?
    I have try believe  me i have try, but it don't work


    ALK
  • Thursday, November 05, 2009 10:14 PMjinzai Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    I tried to show that in my first post. The problem with your WriteAllText is that you have not put the path and the file name together, and you passed the path as the second parameter...that is incorrect. Study this a bit, and see if it is any more clear to you.

    The FileInfo class will give you the full path and name of your file, and WriteAllText will write to such a path.

            Dim fi As New FileInfo("musik.txt")
            Dim fileandpath As String = fi.FullName
    
            '' Array to hold strings from the file
            Dim alltext() As String
            ' This is an easier way to get all of the strings in the file.
            alltext = File.ReadAllLines(fileandpath)
            ' This will append the string to the end of the file.
            My.Computer.FileSystem.WriteAllText(fileandpath, _
                "This is a literal string, or a string variable." & vbCrLf, _
                                                True)
    
    
  • Thursday, November 05, 2009 10:25 PMFrank L. Smith Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    I tried to show that in my first post. The problem with your WriteAllText is that you have not put the path and the file name together, and you passed the path as the second parameter...that is incorrect. Study this a bit, and see if it is any more clear to you.

    The FileInfo class will give you the full path and name of your file, and WriteAllText will write to such a path.

            Dim fi As New FileInfo("musik.txt")
    
            Dim fileandpath As String = fi.FullName
    
    
    
            '' Array to hold strings from the file
    
            Dim alltext() As String
    
            ' This is an easier way to get all of the strings in the file.
    
            alltext = File.ReadAllLines(fileandpath)
    
            ' This will append the string to the end of the file.
    
            My.Computer.FileSystem.WriteAllText(fileandpath, _
    
                "This is a literal string, or a string variable." & vbCrLf, _
    
                                                True)
    
    
    
    


    @Alvin ->

    The above should take care of it. Add in what you need in order to use your OpenFileDialog and you should have it I would think?
  • Thursday, November 05, 2009 10:27 PMRudedog2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I'm not in front of a PC at the moment.
    If Alvin wants to use Open/Save FileDialogs, then show how to pre-select the Application's folder.
    Mark the best replies as answers. "Fooling computers since 1971."
  • Thursday, November 05, 2009 10:40 PMFrank L. Smith Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    I'm not in front of a PC at the moment.
    If Alvin wants to use Open/Save FileDialogs, then show how to pre-select the Application's folder.
    Mark the best replies as answers. "Fooling computers since 1971."

    Rudy,

    Good point.

    @Alvin ->

    Try something like this:

    Dim baseFolder As String = Application.StartupPath
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            ' Button to open the text file
    
            OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt"
            OpenFileDialog1.FilterIndex = 1
            OpenFileDialog1.FileName = baseFolder & "\*.txt"
    
            If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                ' -----------------------
                ' Start the rest here ...
                ' -----------------------
            End If
    
    
        End Sub
    
    • Marked As Answer byAlvin Kuiper Friday, November 06, 2009 9:19 AM
    •  
  • Friday, November 06, 2009 9:19 AMAlvin Kuiper Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Thank you
    now its working.
    And Janzai I'm sorry i could not get you example to work with openfiledialog
    so i have to write again.
    Alvin

    ALK