How to create a file containing files and directories?

前端 未结 2 798
时光说笑
时光说笑 2021-01-07 15:50

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:

  • A XML
2条回答
  •  不思量自难忘°
    2021-01-07 16:41

    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();
                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;
            }
        }
    }
    

提交回复
热议问题