Formular una preguntaFormular una pregunta
 

Respondidawrite / read from a text file

  • miércoles, 04 de noviembre de 2009 20:45Alvin Kuiper Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código
    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

Respuestas

  • jueves, 05 de noviembre de 2009 22:40Frank L. Smith Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     RespondidaTiene código
    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
    
    • Marcado como respuestaAlvin Kuiper viernes, 06 de noviembre de 2009 9:19
    •  

Todas las respuestas

  • miércoles, 04 de noviembre de 2009 20:49ShariqDON Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
  • miércoles, 04 de noviembre de 2009 21:46jinzai Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código

    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)
    
    
  • jueves, 05 de noviembre de 2009 12:52Alvin Kuiper Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código
    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
  • jueves, 05 de noviembre de 2009 16:30JohnWein Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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"
  • jueves, 05 de noviembre de 2009 16:36Rudedog2 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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."
  • jueves, 05 de noviembre de 2009 16:48jinzai Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código
    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)
    
    
    • Editadojinzai jueves, 05 de noviembre de 2009 19:00typos
    •  
  • jueves, 05 de noviembre de 2009 18:49CoolKing Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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...
  • jueves, 05 de noviembre de 2009 20:00Frank L. Smith Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código
    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!

    :)
  • jueves, 05 de noviembre de 2009 21:40Alvin Kuiper Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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
  • jueves, 05 de noviembre de 2009 22:14jinzai Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código

    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)
    
    
  • jueves, 05 de noviembre de 2009 22:25Frank L. Smith Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código

    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?
  • jueves, 05 de noviembre de 2009 22:27Rudedog2 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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."
  • jueves, 05 de noviembre de 2009 22:40Frank L. Smith Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     RespondidaTiene código
    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
    
    • Marcado como respuestaAlvin Kuiper viernes, 06 de noviembre de 2009 9:19
    •  
  • viernes, 06 de noviembre de 2009 9:19Alvin Kuiper Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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