Click here to Skip to main content
15,884,353 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Need a little help, trying to write simple code that will append a file, writing the numbers 1, 2, 3, .... 100 with a For loop. problem I am having is the loop is working but the numbers are all 0s. I am using Console Application for visual basic apart of visual studios 2010. Any suggestions? Here is my code:

VB
Module Module1

    Sub Main()

        Dim counter As Integer = 1
        Dim writer As System.IO.StreamWriter = System.IO.File.AppendText(C:\Documents and Settings\Administrator\My Documents\VB p_files\number_list.dat")

        For counter = 1 To 100
            writer.WriteLine(counter)

        Next
        writer.Close()

        'pause line
        Console.WriteLine("Press enter to continue...")
        Console.ReadLine()
    End Sub

End Module
Posted
Updated 31-Aug-15 6:03am
v2
Comments
Michael_Davies 31-Aug-15 11:53am    
Try : writer.WriteLine(counter.ToString)
Sergey Alexandrovich Kryukov 31-Aug-15 12:07pm    
What's wrong with just reading original MSDN documentation?
—SA
dannybvamp 31-Aug-15 15:11pm    
I got it finally, my vb was sketchy... had to copy and paste code into new project

1 solution

Please see my comment to the question. If you want to use StreamWriter (good idea), you can specify the append mode in a constructor which accepts such Boolean parameter:
https://msdn.microsoft.com/en-us/library/36b035cb%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/f5f5x7kt%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/0wf7ab94%28v=vs.110%29.aspx[^].

Pay attention for the parameter "append".

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900