|
-
Oct 16th, 2006, 04:32 AM
#1
Thread Starter
Hyperactive Member
[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
-
Oct 16th, 2006, 04:47 AM
#2
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);
-
Oct 16th, 2006, 05:18 AM
#3
Thread Starter
Hyperactive Member
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
-
Oct 16th, 2006, 06:54 AM
#4
Re: [Resolved] Serializing List<FileInfo>
Just save the fullpath property, it will save space.
I don't live here any more.
-
Oct 16th, 2006, 08:16 AM
#5
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|