I have a list box that when an item is selected or deselected I want to save the changes to an xml file (so it is always up-to-date on the file and the user does not need a "save" button).
While testing I occasionally am hitting this IOException:
The process cannot access the file 'C:\MyPath\MyFile.xml' because it is being used by another process.
Here is my XML Serialization code:
// Save an object out to the disk
public static void SerializeObject<T>(this T toSerialize, String filename)
{
XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());
TextWriter textWriter = new StreamWriter(filename);
xmlSerializer.Serialize(textWriter, toSerialize);
}
// Load an object from the disk
private static T DeserialzeObject<T>(String filename) where T : class
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
try
{
TextReader textReader = new StreamReader(filename);
return (T)xmlSerializer.Deserialize(textReader);
}
catch (FileNotFoundException)
{ }
return null;
}
And here is how it is called:
// Save off the list because the visibility has changed
public void WorkItemColumnTypeOnVisibleChanged(int fieldID, Visibility visibility)
{
ColumnFields.SerializeObject(ColumnFields.GetSerializeFilename());
}
The deserialize is the one that is giving the error:
WorkItemColumnTypes savedVersion = DeserialzeObject<WorkItemColumnTypes>(result.GetSerializeFilename());
Is there a way to optimize my connections to the file so that I am not tripping over myself?