write an arraylist to a text file

How do I write this out to a text file?  the array is about 1000 lines long.

  Dim arrTSList As New Collection
    Sub Main()
        StartTSRestricted()
        Dim arrUserandName As New ArrayList

        For Each user In arrTSList
            arrUserandName.Add(GetUserInfo(user) & "," & user & "," & GetLastTime(user))
        Next
        arrUserandName.Sort()
 
    End Sub

'need to write out arrUserandName to a txt file
chadmanvbAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
What version VB.Net are you using?...any particular reason for using an ArrayList?

Switch a List(Of String), then use s_chilkury's suggestion with List.ToArray().

Change:

    Dim arrUserandName As New ArrayList

To:

    Dim arrUserandName As New List(Of String)

Then you can use:

    File.WriteAllLines("c:\TextFile.txt", arrUserandName.ToArray)
0
 
deightonCommented:
use a streamwriter

see

http://msdn.microsoft.com/en-us/library/system.io.streamwriter.aspx

except you will do
for each in TheArrayList
0
 
s_chilkuryConnect With a Mentor Commented:
//Try this
using System.IO

File.WriteAllLines("c:\TextFile.txt", arrUserandName)
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
chadmanvbAuthor Commented:
//Try this
using System.IO

File.WriteAllLines("c:\TextFile.txt", arrUserandName)

That does not seem to work for an arraylist

I could not get it to work with streamwriter.  Do you have an example?

0
 
chadmanvbAuthor Commented:
I tried this, but its not working either
  'Dim strTextFile As StreamWriter = New StreamWriter("c:\tsmemberstest.log")

        'For Each line In arrUserandName
            '    strTextFile.WriteLine(line)
        'Next
        'strTextFile.Close()
        'strTextFile.Dispose()
0
 
deightonCommented:
what doesn't work?  is it an error message or failure to compile

have you imported System.IO?

are you using a web site (ASP.net), you may have no authority to "c:\tsmemberstest.log".  It is a problem I had, make some directory and grant all rights to everyone and try writing to there.




Dim strTextFile As  System.IO.StreamWriter = New  System.IO.StreamWriter("c:\tsmemberstest.log")

 For Each MyLine as string In arrUserandName
             strTextFile.WriteLine(MyLine )
Next

strTextFile.Dispose()
0
 
chadmanvbAuthor Commented:
Sorry, did not mean to close this.  wanted to split points with idle and chil.  Can you ples fix
0
 
chadmanvbAuthor Commented:
Ok, got it working thanks so much for the help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.