Solved

write an arraylist to a text file

Posted on 2011-02-17
8
802 Views
Last Modified: 2012-05-11
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
Comment
Question by:chadmanvb
8 Comments
 
LVL 18

Expert Comment

by:deighton
ID: 34918235
use a streamwriter

see

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

except you will do
for each in TheArrayList
0
 
LVL 9

Assisted Solution

by:s_chilkury
s_chilkury earned 250 total points
ID: 34918238
//Try this
using System.IO

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

Author Comment

by:chadmanvb
ID: 34918296
//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
 

Author Comment

by:chadmanvb
ID: 34918467
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 250 total points
ID: 34918723
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
 
LVL 18

Expert Comment

by:deighton
ID: 34918789
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
 

Author Comment

by:chadmanvb
ID: 34918902
Sorry, did not mean to close this.  wanted to split points with idle and chil.  Can you ples fix
0
 

Author Closing Comment

by:chadmanvb
ID: 34918914
Ok, got it working thanks so much for the help.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now