3 Replies - 7303 Views - Last Post: 30 November 2010 - 06:43 PM Rate Topic: -----

#1 ExitWound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-December 09

Saving a ListBox to a TXT File

Posted 30 November 2010 - 04:30 PM

I am currently almost done with my new program which opens, edits, and saves a certain txt file. The only problem I have right now is saving ListBox items to a txt file. If I were to update an existing text file with my program, it would keep the previous data in the txt file and save the data from the ListBox to the txt file. For example, say the txt file has these two lines of code:

One Data Here
Two Data Here

When I edit it in my program to say:

One Data Here
Two Data Here
Three Data Here

This is what is in the txt file after I save using my program:

One Data Here
Two Data Here
One Data Here
Two Data Here
Three Data Here

I'm not exactly sure what I've done wrong but his is the code that I have:

       Dim SetSave As SaveFileDialog = New SaveFileDialog
        Dim i As Integer
        SetSave.Title = "Save SET.txt"
        SetSave.Filter = "SET.txt File (*.txt)|*.txt"

        If SetSave.ShowDialog = Windows.Forms.DialogResult.OK Then
            Dim s As New IO.StreamWriter(SetSave.FileName, True)
            For i = 0 To ListBox1.Items.Count - 1
                s.WriteLine(ListBox1.Items.Item(i))
            Next
            s.Close()
        End If


Is This A Good Question/Topic? 0
  • +

Replies To: Saving a ListBox to a TXT File

#2 xnn  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 227
  • Joined: 10-February 10

Re: Saving a ListBox to a TXT File

Posted 30 November 2010 - 06:17 PM

New IO.StreamWriter(SetSave.FileName, True)

The second parameter of that line tells the stream whether to Append to the file or overwrite it. Passing True causes the file to be appended to. You will need to set that to false. You want want to consider deleting the file before writing to it. since it won't be guaranteed that the entire file will be overwritten. Consider using a FileStream and setting the fileMode to FileMode.Truncate. This will erase the contents of the file upon opening.
Was This Post Helpful? 0
  • +
  • -

#3 ExitWound  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-December 09

Re: Saving a ListBox to a TXT File

Posted 30 November 2010 - 06:34 PM

Alright I set it to false and it said it is being used by a program when I tried to save...

EDIT: Ok nevermind. I'm not sure why it said that but it didn't say it the second time I tried. Thanks for the help =]

This post has been edited by ExitWound: 30 November 2010 - 06:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 xnn  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 227
  • Joined: 10-February 10

Re: Saving a ListBox to a TXT File

Posted 30 November 2010 - 06:43 PM

It may have said it's being used by another process because you didn't close the stream when you opened the file to populate the listbox.

if the file is
xxx
yyy

and you delete only two items in the listbox and add zz without deleting the file or erasing the contents then the overwritten file will contain
zzx
yyy
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1