I am working on a WPF open source markdown editor and I am at the point where I need to save and load documents.
A \"document\" contains 3 files:
You were right to consider zipping the files (you don't want to reinvent that wheel), and you were also right that unzipping to the filesystem is ugly. But you don't have to; you can create the zip and pull files out of it entirely in your own code with the ZipArchive class.
If anyone is wondering how I applied Ed Plunkett's answer, here is the code:
var saveDialog = new SaveFileDialog
{
CreatePrompt = true,
OverwritePrompt = true,
Filter = "Project Markdown File | *.pmd"
};
var result = saveDialog.ShowDialog();
if (result != null)
{
if (result == true)
{
if (!Directory.Exists(saveDialog.FileName + "_temp"))
{
var parentFolder = Directory.CreateDirectory(saveDialog.FileName + "_temp").FullName;
var mp = new MarkdownParser();
// Generate HTML
var html = mp.Parse(document.Markdown.Markdown);
var markdownFilePath = parentFolder + "\\" + saveDialog.SafeFileName + ".md";
var htmlFilePath = parentFolder + "\\" + saveDialog.SafeFileName + ".html";
var metadataFilePath = parentFolder + "\\" + saveDialog.SafeFileName + ".xml";
// Generate MD file
using (var sw = new StreamWriter(markdownFilePath))
{
sw.Write(document.Markdown.Markdown);
}
// Generate HTML file
using (var sw = new StreamWriter(htmlFilePath))
{
sw.Write(html);
}
// Generate XML file
document.Metadata.FileName = saveDialog.SafeFileName;
var gxs = new GenericXmlSerializer<DocumentMetadata>();
gxs.Serialize(document.Metadata, metadataFilePath);
// Generate style
var cssFilePath = AppDomain.CurrentDomain.BaseDirectory + "Styles\\github-markdown.css";
if (!Directory.Exists(parentFolder + "\\Styles"))
{
Directory.CreateDirectory(parentFolder + "\\Styles");
}
if (!File.Exists(parentFolder + "\\Styles\\github-markdown.css"))
{
File.Copy(cssFilePath, parentFolder + "\\Styles\\github-markdown.css");
}
// Generate the package
ZipFile.CreateFromDirectory(parentFolder, saveDialog.FileName);
// Update the view
var saveResult = new SaveResult
{
FileName = saveDialog.SafeFileName,
Source = htmlFilePath.ToUri(),
TempFile = saveDialog.FileName + "_temp"
};
return saveResult;
}
}
}