Results 1 to 3 of 3

Thread: [RESOLVED] Reading Files and Placing them in a Collection

  1. #1

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    [RESOLVED] Reading Files and Placing them in a Collection

    I have created football (American) play creator. When the user is satisfied with a play he has created it is saved it as a file with a .play extension. What I want to do is to cycle through all the files in a folder (probably named after the team), and place each one that has the .play extension into a CList (of ClPlay) collection.

    I have been able to successfully load one play at a time by using this code...
    Code:
                Dim fs As Stream = New FileStream(diaOpenPlay.FileName, FileMode.Open)
                Dim bf As Runtime.Serialization.Formatters.Binary.BinaryFormatter = New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
                Dim Play As CLPlay
                Play = CType(bf.Deserialize(fs), CLPlay)
    ...but that was when a specific play was selected from a load dialog box. This time I want to load all the plays in a folder and store it in a collection, and I'm not sure how to modify the above code to do that.

    Thanks in advance.
    Last edited by neef; Jul 23rd, 2009 at 07:04 PM.
    Intermediate Level Programmer Extraordinaire

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: Reading Files and Placing them in a Collection

    Use System.IO.Directory.GetFiles("folder path here", "*.play") to get all the .play files into an array and then loop thru that array. Each string in the array is the full path to a .play file, so you use the same code you have for a single .play file (posted above) to create a CLPlay object and add that to the List(Of CLPLay) or a Dictionary(Of String, CLPLay). I prefer the Dictionary in this case because you can assign a name (key) to each CLPLay object so that later you can use that key to get to the coresponding CLPLay object.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  3. #3

    Thread Starter
    Hyperactive Member neef's Avatar
    Join Date
    Dec 2001
    Location
    Boston
    Posts
    311

    Re: Reading Files and Placing them in a Collection

    Totally nailed it.

    Thanks so much!
    Intermediate Level Programmer Extraordinaire

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width