Finishing File Opening and Saving
I finished the file open method as such:
private void msiFileOpen_Click(object sender, EventArgs e)
{
string file = this.GetFileFromDialog(this.ofdDialog);
if (!string.IsNullOrEmpty(file))
{
StreamReader reader = new StreamReader(file);
string xml = reader.ReadToEnd();
if (!string.IsNullOrEmpty(xml))
{
MetadataSerialization<Task> serializer = new MetadataSerialization<Task>();
//Clear the collection of any existing items
MGR.Tasks.Clear();
//Copy the tasks from the collection returned from serialization
MGR.Tasks.CopyFrom(serializer.LoadFromXml(xml));
//Save the path to the file that was opened
_openedFile = file;
}
else
MessageBox.Show("The file you selected is not a valid task file");
}
}
I have a GetFileFromDialog which gets the path of the file from the parameter dialog box. Then, if a file has been provided, I read the file, and make sure we actually have data; when cancelling the dialog box, a value will not be provided, so we must account for that. When we do, I deserialize it, and pass the tasks to the root Manager object (the object model object). This loads the tasks into the editor. _openedFile will be discussed later.
Save File operations looks like this:
private void msiFileSave_Click(object sender, EventArgs e)
{
string file;
//Get the path of a file from one of the sources
if (!string.IsNullOrEmpty(_openedFile))
file = _openedFile;
else
file = this.GetFileFromDialog(this.sfdDialog);
//if the file has been selected
if (!string.IsNullOrEmpty(file))
{
MetadataSerialization<Task> serializer = new MetadataSerialization<Task>();
//Save the tasks to a specific file
serializer.SaveToXmlFile(MGR.Tasks, file);
}
}
The _openedFile variable stores the path to the file that was opened; if a new task list, then it will be null, otherwise containing the field. So, from this, if it exists, I get the path to the file and store it in a variable; otherwise, I get the file from the dialog box. If no file exists, then don't proceed, otherwise, the collection is saved to XML.
I run it in the application, and what do you know, it works great. That was really easy to implement, besides some minor problems with serialization. These features have been completed as of now.