none
how EDIT a .txt file?

    Question

  • Hello, i want to EDIT an existing .txt  file, but i dont want remove the lines above the new lines, i wish do it, on visual basic. how can i do it??

    thanks..
    Saturday, December 12, 2009 11:04 PM

Answers

  • what are you adding to the file - single lines or a block of text?

    for example, to write a single line using StreamWriter

    the Boolean parameter in the arguments is to Append. If it is True it adds the line to the end; if False, it overwrites the entire file

    Using sw As StreamWriter = New StreamWriter("C:\Users\Joe\Desktop\Test Folder\swTest.txt", True)
                sw.WriteLine(TextBox1.Text)
            End Using



    • Proposed as answer by •.trujade.• Sunday, December 13, 2009 12:20 AM
    • Marked as answer by sowilosol Sunday, December 13, 2009 8:57 PM
    Saturday, December 12, 2009 11:20 PM

All replies

  • You might want to do this:

    Add either a TextBox (set to show multiple lines and set the scroll bar for vertical) or a RichTextBox and read your text file into it.

    Make all the modifications that you want, then in a button event handler, overwrite the existing text file with your revised one. Be sure that nothing else has the file "open" when you go to overwrite it as might be the case depending on how you're reading the file in, but we'll get into that if that turns out to be the case.

    Good luck! :)
    Saturday, December 12, 2009 11:15 PM
  • what are you adding to the file - single lines or a block of text?

    for example, to write a single line using StreamWriter

    the Boolean parameter in the arguments is to Append. If it is True it adds the line to the end; if False, it overwrites the entire file

    Using sw As StreamWriter = New StreamWriter("C:\Users\Joe\Desktop\Test Folder\swTest.txt", True)
                sw.WriteLine(TextBox1.Text)
            End Using



    • Proposed as answer by •.trujade.• Sunday, December 13, 2009 12:20 AM
    • Marked as answer by sowilosol Sunday, December 13, 2009 8:57 PM
    Saturday, December 12, 2009 11:20 PM
  • what are you adding to the file - single lines or a block of text?

    for example, to write a single line using StreamWriter

    the Boolean parameter in the arguments is to Append. If it is True it adds the line to the end; if False, it overwrites the entire file

    Using sw As StreamWriter = New StreamWriter("C:\Users\Joe\Desktop\Test Folder\swTest.txt", True)
    
                sw.WriteLine(TextBox1.Text)
    
            End Using
    
    






    or ... My.Computer.FileSystem.WriteAllText(...)

    Joe I've noticed that you tend not to use the "My.Computer.FileSystem" namespace. I think it's easier - am I missing something important? I wasn't sent the memo :-o
    Saturday, December 12, 2009 11:32 PM
  • Saturday, December 12, 2009 11:42 PM
  • Well, one of things that an MVP said in that is: "...true so in this case, you are better, in general anyway, to use a StringBuilder to append strings then finally call .ToString() on the object when time comes to giving the WriteAllText the string to write to file", and that sure makes sense.

    I still don't see how one is better than another though.

    Wasn't the "My" added as an improvement over previous methods? Or was I reading someone's opinion assuming it to be factual, but not?




    ...by the way I didn't mean to rob the thread here. I hope the OP will overlook my rants. :-)
    Saturday, December 12, 2009 11:48 PM
  • well, the stringbuilder part is kind of a separate issue - he's just saying build your string first, then write it, rather than write it piece by piece. But that all depends on what you are writing - and you could use the same stringbuilder with StreamWriter. But say for example i want to iterate through the items in a ListBox and write each one. I'm just going to use sw.WriteLine for each item. I don't see the need to use stringbuilder

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim sb As StringBuilder = New StringBuilder
            sb.Append("hello" & vbCrLf)
            sb.Append("world")
            Using sw As StreamWriter = New StreamWriter("C:\Users\Joe\Desktop\Test Folder\swTest.txt", True)
                sw.Write(sb.ToString)
            End Using
    
        End Sub



    and you are correct - they're about equal - but since WriteAllText uses StreamWriter underneath...

    I think what is being referred to with the My namespace is the difference between My.Computer.FileSystem and the FileSystem module

    in a Button click event, type FileSystem  so IntelliSense shows you Module FileSystem

    read what it says - you may have to erase a character to get it to come up again - it's a little long

    so what it comes down to is, use the one you want. I just happen to use StreamWriter/StreamReader. I just got in the habit of using it over FileSystem WriteAllText, no other reason

    Good question, Frank :)
    Sunday, December 13, 2009 12:12 AM

  • Wasn't the "My" added as an improvement over previous methods? Or was I reading someone's opinion assuming it to be factual, but not?


    You will find quite an amount of prejudice against use of the My namespace.  The typical argument is that it hides the detail so the user never comes to understand what's happening behind the scenes.   Apparently, that tends to create bad programmers.   It's a point of view that makes no sense to me at all.
    Sunday, December 13, 2009 12:13 AM
  • Joe and Acamar,

    This is worthy of another discussion in another thread. I had no idea what I started here!





    Sowilosol: I didn't mean to hijack your thread here but one thing that I hope you do see is that this place is a veritable wealth of knowledge!

    Good luck with your project. :)

    Sunday, December 13, 2009 12:19 AM
  • Acamar,

    yeah, I think there is a lot of good stuff in the My Namespace. I believe it helps facilitate accessing many of the methods, etc that people would use.

    And I have no problem using it - I just use StreamReader/StreamWriter.

    what is your preference for this task?


    Frank
    well, this is good because the OP can see there are different ways to do something, and can make a decision which to use.
    Sunday, December 13, 2009 12:38 AM
  • Then simply create two boxes above each other (the one above read only) (text boxes because those are for txt files).

    Use a streamreader to read the box above (and close it then direct again)

    If the text in the second box is ready then use a button to write it back with a streamwriter to the original file with the append property set to true.


    Success
    Cor
    Sunday, December 13, 2009 1:13 PM
  • the first answer of jwavila did help me.. thanks to everybody!!
    Sunday, December 13, 2009 9:01 PM