Results 1 to 5 of 5

Thread: [Resolved] Serializing List<FileInfo>

  1. #1

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Question [Resolved] Serializing List<FileInfo>

    Hello all, i'm facing problems to serialize a generic list of FileInfo classes. My problem is that I need to retrieve the list of files in a directory and then save that list. The idea is to serialize that list and deserialize it when needed. i'm not sure if its a right thing to serialize a whole fileinfo class or would it be easier for have a custom class holding only the filenames, any suggestion in that sense would be most welcomed. Anyways here's my piece of code:

    Code:
    // declared in a form
    private List<FileInfo> Files = new List<FileInfo>();
    
    // in some method
    
    
    // following line giving me error saying cannot serialize since FileInfo does not have a parameterless constructor... ??? 
    XmlSerializer xSr = new XmlSerializer(typeof(List<FileInfo>));
    Stream stm = new FileStream("c:\\Test", FileMode.CreateNew);
    
    xSr.Serialize(stm, Files);
    stm.Close();
    Any ideas???
    Last edited by vbud; Oct 16th, 2006 at 05:20 AM. Reason: Resolved
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Serializing List<FileInfo>

    Do you need anything more than the file names? If not then an array of strings is all you need:
    Code:
    string[] files = System.IO.Directory.GetFiles(@"folder path here");
    System.Xml.Serialization.XmlSerializer serialiser = new System.Xml.Serialization.XmlSerializer(typeof(string[]));
    System.IO.FileStream stream = new System.IO.FileStream(@"file path here", System.IO.FileMode.Create);
    
    serialiser.Serialize(stream, files);
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member vbud's Avatar
    Join Date
    Jan 2002
    Location
    Mru 20 17S, 57 33E Goal: Get out of the BOX Status: In The Shadows!!! Target Posts: 3,000,000,000
    Posts
    378

    Re: Serializing List<FileInfo>

    Yep, I ended up doing a custom serializable class storing only the FileName, Extension and Fullpath properties. Seems that serializing a List<CustomClass> works well. Thanks for the help anyway
    >!v!<
    Free your mind, stop thinking
    http://inspirone.blogspot.com

    Please rate this post if it helped you

  4. #4
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [Resolved] Serializing List<FileInfo>

    Just save the fullpath property, it will save space.
    I don't live here any more.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [Resolved] Serializing List<FileInfo>

    As woss says, there's no point storing the full path and then two parts of the full path that you could easily get with the IO.Path.GetFileNameWithoutExtension and IO.Path.GetExtension methods anyway.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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