Solved

write an arraylist to a text file

Posted on 2011-02-17
8
831 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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