[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. :)
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.
Re: Reading Files and Placing them in a Collection
Totally nailed it. :)
Thanks so much! :)