Results 1 to 7 of 7

Thread: Writing to file problem...

  1. #1
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    296

    Writing to file problem...

    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???

    Code:
    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...
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  2. #2
    Bob Langlade's Avatar
    Bob Langlade is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2005
    Location
    Saint-Pierre & Miquelon Islands
    Posts
    115
    Reputation
    144
    Code:
            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

  3. #3
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    296
    Ok, looks like I was close, not that close counts!
    Thanks...
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  4. #4
    newguy's Avatar
    newguy is offline VB.NET Forum Idol
    .NET Framework
    .NET 3.5 (VS 2008)
    Join Date
    Jun 2008
    Location
    Denver Co, USA
    Posts
    611
    Reputation
    296
    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...

    Code:
    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 by newguy; 09-09-2008 at 12:39 AM.
    Close Counts for Horseshoes, Handgranades, and Nuclear Missiles!

  5. #5
    Bob Langlade's Avatar
    Bob Langlade is offline VB.NET Forum Fanatic
    .NET Framework
    .NET 4.0
    Join Date
    Sep 2005
    Location
    Saint-Pierre & Miquelon Islands
    Posts
    115
    Reputation
    144
    Yes it's working, but you can convert your item to string only once.

    Code:
            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

  6. #6
    FluidOne is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Nov 2010
    Posts
    26
    Reputation
    89
    Dont create a string variable everytime SelectedIndexChange occurs, huge waste.

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

  7. #7
    FluidOne is offline VB.NET Forum Newbie
    .NET Framework
    .NET 3.5
    Join Date
    Nov 2010
    Posts
    26
    Reputation
    89
    Quote Originally Posted by FluidOne View Post
    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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •