7 Replies - 626 Views - Last Post: 15 January 2013 - 10:57 PM Rate Topic: -----

#1 koolazngy94  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 119
  • Joined: 29-July 08

Streamwriter doesn't write?

Posted 14 January 2013 - 11:25 PM

I been working on this thing all day today, and I finally gave up and decided to ask for help from you guys. Basically, whenever I hit Add, it just doesn't write in a text file.

fListView.Items.Add(fname.Text).SubItems.Add(femail.Text)

        Try

            w = New IO.StreamWriter(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "/flist.txt", True)

            Dim ii As Integer
            Dim line As String = r.ReadLine
            For ii = 0 To fListView.Items.Count - 1
                w.WriteLine(fListView.Items.Item(ii).Text & ":" & fListView.Items.Item(ii).SubItems.Item(ii).Text)
            Next
            w.Close()

        Catch
        End Try



I am using Listview that has 2 columns.

What am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: Streamwriter doesn't write?

#2 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Streamwriter doesn't write?

Posted 15 January 2013 - 02:29 AM

imports system.io

dim w as streamwriter
dim Txtfile as string
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       
        ListView1.Items.Add(fname.Text).SubItems.Add(femail.Text)

        Try

            TxtFile = "C:\Temp\kai.txt"
            w = File.CreateText(TxtFile)

           

            Dim ii As Integer

            For ii = 0 To ListView1.Items.Count - 1



                w.WriteLine(ListView1.Items.Item(ii).Text & "  :  " & ListView1.Items.Item(ii).SubItems.Item(1).Text)

            Next ii

            w.Close()



        Catch

        End Try

    End Sub


This post has been edited by kai_itz me: 15 January 2013 - 02:30 AM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3252
  • View blog
  • Posts: 10,902
  • Joined: 12-December 12

Re: Streamwriter doesn't write?

Posted 15 January 2013 - 05:43 AM

Within your loop I assume you want to refer to the first sub-item:

w.WriteLine(fListView.Items.Item(ii).Text & ":" & fListView.Items.Item(ii).SubItems.Item(1).Text)


but I haven't looked beyond this.

Personally, I would leave the Try..Catch statement for a while as, initially, you want to see all error messages.

This post has been edited by andrewsw: 15 January 2013 - 05:45 AM

Was This Post Helpful? 0
  • +
  • -

#4 koolazngy94  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 119
  • Joined: 29-July 08

Re: Streamwriter doesn't write?

Posted 15 January 2013 - 05:03 PM

Well, it worked for the first time, but then I get another error saying that it is being used already. I already did w.Close and other Stream close, like r.Close, but it wouldn't work. Any suggestion?
Was This Post Helpful? 0
  • +
  • -

#5 lar3ry  Icon User is offline

  • Coding Geezer
  • member icon

Reputation: 310
  • View blog
  • Posts: 1,290
  • Joined: 12-September 12

Re: Streamwriter doesn't write?

Posted 15 January 2013 - 07:42 PM

View Postkoolazngy94, on 15 January 2013 - 06:03 PM, said:

Well, it worked for the first time, but then I get another error saying that it is being used already. I already did w.Close and other Stream close, like r.Close, but it wouldn't work. Any suggestion?

I will assume that "it" refers to the StreamWriter or StreamReader.

Try a w.Dispose and r.Dispose, then when you want to Write or Read again, use New ones.
Was This Post Helpful? 0
  • +
  • -

#6 Pwn  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 458
  • Joined: 25-November 07

Re: Streamwriter doesn't write?

Posted 15 January 2013 - 08:41 PM

Koolazngy, kai_itz_me answered your question, I think. Your code for adding items should be nested within a button click event handler, otherwise, there's nothing telling the program when to add items. The code you posted doesn't show a button_click event handler.
Was This Post Helpful? 0
  • +
  • -

#7 koolazngy94  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 119
  • Joined: 29-July 08

Re: Streamwriter doesn't write?

Posted 15 January 2013 - 09:38 PM

View PostPwn, on 15 January 2013 - 08:41 PM, said:

Koolazngy, kai_itz_me answered your question, I think. Your code for adding items should be nested within a button click event handler, otherwise, there's nothing telling the program when to add items. The code you posted doesn't show a button_click event handler.


I just got it to work.

Thank you kai_itz_me for fixing the code up, and thank you lar3ry for suggesting to put w.close()/r.close() before and not after. It works like a charm!
Was This Post Helpful? 0
  • +
  • -

#8 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,060
  • Joined: 02-July 08

Re: Streamwriter doesn't write?

Posted 15 January 2013 - 10:57 PM

The Using blocks are great for this type of work.

Using sw As New StreamWriter(...)
  'do stuff
End Using 'stream is closed and disposed here.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1