VB.net: overwrite everything in a text file











up vote
-2
down vote

favorite












in my VB.net Application id like to overwrite and add new content of a text file
What Code do I need to use?
Thanks










share|improve this question






















  • You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
    – the_lotus
    Nov 21 at 17:32












  • as i said, a .txt file, and normal strings as data (plain text)
    – user99170
    Nov 21 at 17:37






  • 2




    You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
    – the_lotus
    Nov 21 at 17:40










  • Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
    – Mary
    Nov 21 at 18:03










  • Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
    – jmcilhinney
    Nov 22 at 2:30















up vote
-2
down vote

favorite












in my VB.net Application id like to overwrite and add new content of a text file
What Code do I need to use?
Thanks










share|improve this question






















  • You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
    – the_lotus
    Nov 21 at 17:32












  • as i said, a .txt file, and normal strings as data (plain text)
    – user99170
    Nov 21 at 17:37






  • 2




    You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
    – the_lotus
    Nov 21 at 17:40










  • Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
    – Mary
    Nov 21 at 18:03










  • Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
    – jmcilhinney
    Nov 22 at 2:30













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











in my VB.net Application id like to overwrite and add new content of a text file
What Code do I need to use?
Thanks










share|improve this question













in my VB.net Application id like to overwrite and add new content of a text file
What Code do I need to use?
Thanks







vb.net






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 17:30









user99170

1




1












  • You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
    – the_lotus
    Nov 21 at 17:32












  • as i said, a .txt file, and normal strings as data (plain text)
    – user99170
    Nov 21 at 17:37






  • 2




    You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
    – the_lotus
    Nov 21 at 17:40










  • Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
    – Mary
    Nov 21 at 18:03










  • Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
    – jmcilhinney
    Nov 22 at 2:30


















  • You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
    – the_lotus
    Nov 21 at 17:32












  • as i said, a .txt file, and normal strings as data (plain text)
    – user99170
    Nov 21 at 17:37






  • 2




    You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
    – the_lotus
    Nov 21 at 17:40










  • Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
    – Mary
    Nov 21 at 18:03










  • Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
    – jmcilhinney
    Nov 22 at 2:30
















You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
– the_lotus
Nov 21 at 17:32






You could start looking here. There's a lot of piece missing. What type of file, what type of data, what's the new content, ...
– the_lotus
Nov 21 at 17:32














as i said, a .txt file, and normal strings as data (plain text)
– user99170
Nov 21 at 17:37




as i said, a .txt file, and normal strings as data (plain text)
– user99170
Nov 21 at 17:37




2




2




You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
– the_lotus
Nov 21 at 17:40




You can ReadAllText of the file. Do the changes from the resulting string. Then WriteAllText back to the file.
– the_lotus
Nov 21 at 17:40












Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
– Mary
Nov 21 at 18:03




Check out File.Open(String, FileMode) Then check FileMode. It looks like the Truncate field of FileMode might do what you want.
– Mary
Nov 21 at 18:03












Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
– jmcilhinney
Nov 22 at 2:30




Do some research on reading and writing text files in VB.NET. There's loads of information around. When you open a file for writing, the only options are overwriting or appending so there's no great mystery to how to overwrite. It's the default in most cases. You should ALWAYS be looking for existing information first and then ask here if you aren't able to make what you've learned work. SO is not a place to ask a question as the first option.
– jmcilhinney
Nov 22 at 2:30












1 Answer
1






active

oldest

votes

















up vote
2
down vote













Read (ie: load) everything in the TXT file into your program.



Dim sFullPathToFile As String = Application.StartupPath & "Sample.txt"

Dim sAllText As String = ""
Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
sAllText = xStreamReader.ReadToEnd
End Using
Dim arNames As String() = Split(sAllText, vbCrLf)

'Just for fun, display the found entries in a ListBox
For iNum As Integer = 0 To UBound(arNames)
If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
Next iNum


Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).



'Use the True to indicate it is to be appended to existing file
'Or use False to open the file in Overwrite mode
Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)

'Use the carriage return character or else each entry is on the same line
xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
xStreamWRITER.Close()





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417651%2fvb-net-overwrite-everything-in-a-text-file%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Read (ie: load) everything in the TXT file into your program.



    Dim sFullPathToFile As String = Application.StartupPath & "Sample.txt"

    Dim sAllText As String = ""
    Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
    sAllText = xStreamReader.ReadToEnd
    End Using
    Dim arNames As String() = Split(sAllText, vbCrLf)

    'Just for fun, display the found entries in a ListBox
    For iNum As Integer = 0 To UBound(arNames)
    If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
    Next iNum


    Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).



    'Use the True to indicate it is to be appended to existing file
    'Or use False to open the file in Overwrite mode
    Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)

    'Use the carriage return character or else each entry is on the same line
    xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
    xStreamWRITER.Close()





    share|improve this answer

























      up vote
      2
      down vote













      Read (ie: load) everything in the TXT file into your program.



      Dim sFullPathToFile As String = Application.StartupPath & "Sample.txt"

      Dim sAllText As String = ""
      Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
      sAllText = xStreamReader.ReadToEnd
      End Using
      Dim arNames As String() = Split(sAllText, vbCrLf)

      'Just for fun, display the found entries in a ListBox
      For iNum As Integer = 0 To UBound(arNames)
      If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
      Next iNum


      Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).



      'Use the True to indicate it is to be appended to existing file
      'Or use False to open the file in Overwrite mode
      Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)

      'Use the carriage return character or else each entry is on the same line
      xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
      xStreamWRITER.Close()





      share|improve this answer























        up vote
        2
        down vote










        up vote
        2
        down vote









        Read (ie: load) everything in the TXT file into your program.



        Dim sFullPathToFile As String = Application.StartupPath & "Sample.txt"

        Dim sAllText As String = ""
        Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
        sAllText = xStreamReader.ReadToEnd
        End Using
        Dim arNames As String() = Split(sAllText, vbCrLf)

        'Just for fun, display the found entries in a ListBox
        For iNum As Integer = 0 To UBound(arNames)
        If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
        Next iNum


        Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).



        'Use the True to indicate it is to be appended to existing file
        'Or use False to open the file in Overwrite mode
        Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)

        'Use the carriage return character or else each entry is on the same line
        xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
        xStreamWRITER.Close()





        share|improve this answer












        Read (ie: load) everything in the TXT file into your program.



        Dim sFullPathToFile As String = Application.StartupPath & "Sample.txt"

        Dim sAllText As String = ""
        Using xStreamReader As StreamReader = New StreamReader(sFullPathToFile)
        sAllText = xStreamReader.ReadToEnd
        End Using
        Dim arNames As String() = Split(sAllText, vbCrLf)

        'Just for fun, display the found entries in a ListBox
        For iNum As Integer = 0 To UBound(arNames)
        If arNames(iNum) > "" Then lstPeople.Items.Add(arNames(iNum))
        Next iNum


        Because you wanted to overwrite everything in the file, we now use StreamWriter (not a StreamReader like before).



        'Use the True to indicate it is to be appended to existing file
        'Or use False to open the file in Overwrite mode
        Dim xStreamWRITER As StreamWriter = New StreamWriter(sFullPathToFile, False)

        'Use the carriage return character or else each entry is on the same line
        xStreamWRITER.Write("I have overwritten everything!" & vbCrLf)
        xStreamWRITER.Close()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 16:03









        ThePeter

        308111




        308111






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53417651%2fvb-net-overwrite-everything-in-a-text-file%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks