Solved

write an arraylist to a text file

Posted on 2011-02-17
8
844 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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
 
LVL 86

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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

635 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