Writing to file problem...

newguy

Well-known member
Joined
Jun 30, 2008
Messages
611
Location
Denver Co, USA
Programming Experience
1-3
Hi All.
Trying to save listbox.items(text) to a file and I keep getting a error - unable to process the file as another process is using it. So what am I doing wrong???

VB.NET:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim file_name As String = "c:" & txtFileName.Text & ".txt"
        System.IO.File.Create(file_name)
        If System.IO.File.Exists(file_name) = True Then
            Dim objWriter As New System.IO.StreamWriter(file_name)
            Dim int As Object
            int = ListBox2.Items.ToString
            objWriter.Write(int)
            objWriter.Write("This is a test")
            objWriter.Close()
        End If
    End Sub

Thanks...
 
VB.NET:
        Dim file_name As String = "c:\Test.txt"
        Try
            Dim objWriter As New System.IO.StreamWriter(file_name)
            For Each item In ListBox2.Items
                objWriter.WriteLine(item.ToString)
            Next
            objWriter.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 
Bob do you have time for another question...
...If I send that file_name to the listbox on a different form (I can do this part) when I select the index how can I pull up the file in the texbox1 (just below the listbox)? Opps I might have found it... it seems to work, will have to try it with more than 1 file in the listbox later...

VB.NET:
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Try
            Dim item As String
            item = ListBox1.SelectedItem
            If System.IO.File.Exists(item.ToString) = True Then
                Dim objReader As New System.IO.StreamReader(item.ToString)
                TextBox1.Text = objReader.ReadToEnd
            End If
        Catch
        End Try
    End Sub

Thanks again Bob...
 
Last edited:
Yes it's working, but you can convert your item to string only once.

VB.NET:
        TextBox1.Text = "" 'To Clear the textbox if the file doesn't exist
        Dim Item As String = ListBox1.SelectedItem.ToString
        If System.IO.File.Exists(Item) = True Then
            Dim objReader As New System.IO.StreamReader(Item)
            TextBox1.Text = objReader.ReadToEnd
        End If
 
Dont create a string variable everytime SelectedIndexChange occurs, huge waste.

try
If System.IO.File.Exists(listbox1.SelectedItem)=true the
...
 
Dont create a string variable everytime SelectedIndexChange occurs, huge waste.

try
If System.IO.File.Exists(listbox1.SelectedItem)=true the

You may not want to read an entire file into a textbox everytime the listbox SelectedIndexChange event occurs!

...

Attach a read file button.
Attach a popup menu to the listbox.
But do change the code. Unless of course your listbox only has one item in it.
 
Back
Top