Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 855
  • Last Modified:

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
0
chadmanvb
Asked:
chadmanvb
2 Solutions
 
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_chilkuryCommented:
//Try this
using System.IO

File.WriteAllLines("c:\TextFile.txt", arrUserandName)
0
 
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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
 
Mike TomlinsonMiddle 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:
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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now