[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???
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);
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 :)
Re: [Resolved] Serializing List<FileInfo>
Just save the fullpath property, it will save space.
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.