Jump to content
xisto Community
Sign in to follow this  
foto51

How Do I Save Files To .txt In Vb.net

Recommended Posts

Hello, I have a project that I am doing. I have a list box that you can enter data in, and I need it to have the ability to save that list into a text file (Names.txt). The txt file should have each entry on it's own line. Any help would be appreciated.

Share this post


Link to post
Share on other sites

I'm not sure about Vb.Net, I haven't touched it in couple of years, but I can give you code, how it would look in VB6, and you can go from there:

Dim i As LongOpen "Names.txt" For Output As #1  For i = 0 To List1.ListCount - 1    Print #1, List1.List(i)  Next iClose #1

And that's it... I believe it should be fairly simple in Vb.Net too... Probably very similar...

Share this post


Link to post
Share on other sites

OK, I'm sure you already found what you were looking for, but I'll post an example code, of how to write text files in VB.NET... Maybe someone in the future would find it usefull:

Imports SystemImports System.IOPublic Class Form1  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click	Dim f As StreamWriter, i As Long, obj As Object	For i = 1 To 10	  ListBox1.Items.Add("Item " & i.ToString)	Next i	f = New StreamWriter("C:\Test.txt")	For i = 0 To ListBox1.Items.Count - 1	  f.WriteLine(ListBox1.Items.Item(i).ToString)	Next i	f.Close()  End SubEnd Class

That's the full working code, just copy and paste it in your project, and voila... Hope it helped a bit :lol:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.