Home All Groups Group Topic Archive Search About

how to record the contents of a listebox in a text

Author
23 May 2006 6:01 AM
Pascal
hello
i would like to populate a listbox with words from a list (using
openfiledialog) this list is made with one word per line like this :

manger
finir
mourir
courir
etc...

to open the file :

i tried this with fileopen until i save because a message comes telling me
than another process already use this file . here is the code :
###################################################
'Dim i As Short
'FileOpen(1, My.Application.Info.DirectoryPath & "\" & "a.txt",
OpenMode.Output)
'For i = 0 To lstInputWord.Items.Count - 1
'LaListe.SelectedIndex = i
'PrintLine(1, LaListe.Text)
'Next
'FileClose(1)
############################################"
so i changed and try with streamreader this code :

If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    Try

                        Dim fileReader As System.IO.StreamReader
                        fileReader =
My.Computer.FileSystem.OpenTextFileReader(.FileName,
System.Text.Encoding.Default)
                        Dim stringReader As String = ""
                        lstInputWord.Items.Clear()
                        While Not fileReader.EndOfStream
                            stringReader = fileReader.ReadLine()
                            lstInputWord.Items.Add(stringReader)
                        End While
                        fileReader.Close()
                    Catch fileException As Exception
                        Throw fileException
        End Try
##########################################

it seems to work fine like this but i am not able to change this code :
####################################################"
save and close the file :
                    'Dim i As Short
                    'FileOpen(1, My.Application.Info.DirectoryPath & "\" &
"a.txt", OpenMode.Output)
                    'For i = 0 To lstInputWord.Items.Count - 1
                    'LaListe.SelectedIndex = i
                    'PrintLine(1, LaListe.Text)
                    'Next
                    'FileClose(1)
###########################################
by the equivalent with streamwriter.....

so now i would like to try with  stream writer but i didn't find the good
way to use it... somebody can help me
thanks


http://www.scalpa.info

Author
23 May 2006 9:48 PM
Shane
If I read this right, you want to know how to create / replace a text
file using streamwriter. You should use the appropriate try block
around this code.

Dim i as integer
Dim strFile as string = "YourFileName"

If File.Exists(strFile) Then Kill(strFile)
Dim sw As New StreamWriter(strfile)
For i = 1 to 10
  sw.WriteLine(i.ToString)
Next i
sw.Close()