c# - 将多种文件类型保存到一个文件中

标签 c# .net wpf xaml

我正在开发一个 3D 应用程序,用户可以在其中加载多个图像,我使用的 3D 库具有将整个场景导出为 XAML 的功能,当然在 XAML 中我只有图像的路径,所以文件只在创建它的机器上有效,我想做的是保存 XAML,将 XAML 中的图像路径更改为相对于 XAML 文件路径,保存图像XAML 在同一个新文件中,这样新文件将始终包含 XAML 和图像。如何在同一文件中保存多种文件类型?对整个想法的任何其他建议将不胜感激。

最佳答案

如果您使用的是 .NET 4.5,则可以使用新的 ZipFile class 以编程方式将多个文件轻松保存到一个 zip 文件夹中.

首先,您需要将文件准备到一个新文件夹(Directory.CreateDirectoryFile.Move),然后您可以简单地使用 ZipFile.CreateFromDirectory Method (改编自链接页面):

string filePathOfNewFolder = @"c:\example\start";
string zipFilePath = @"c:\example\result.zip";

ZipFile.CreateFromDirectory(filePathOfNewFolder, zipFilePath);

更新>>>

对于 .NET 4,您可能不得不更多地依赖第三方库。您可以从 DotNetZip - Zip and Unzip in C#, VB, any .NET language 下载 DotNetZip 库CodePlex 上的页面。它也相当简单。这是从链接页面中获取的示例:

using (ZipFile zip = new ZipFile())
{
    // add this map file into the "images" directory in the zip archive
    zip.AddFile("c:\\images\\personal\\7440-N49th.png", "images");
    // add the report into a different directory in the archive
    zip.AddFile("c:\\Reports\\2008-Regional-Sales-Report.pdf", "files");
    zip.AddFile("ReadMe.txt");
    zip.Save("MyZipFile.zip");
}

关于c# - 将多种文件类型保存到一个文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23521992/

相关文章:

c# - 如何从 Linq 中的配置文件的 Appsettings 获取键和值

c# - 将查询字符串传递给 mvc View 操作

c# - 使用 JsonConverterAttribute 装饰类时使用默认的 JsonSerializer

wpf - 自定义事件设计器中的参数验证

wpf - WPF UserControl 中的控件不会引起丢失的焦点

c# - 不显示wpf边框之外的内容

c# - 使用 HTML AGILITY PACK 更改标签内部文本

c# - DataGridView 不需要调用 EndNew

asp.net - ASP.NET 5、.NET Core 和 ASP.NET Core 5 之间有什么区别?

c# - 有没有办法在 .Net 中创建 "Self-hosted"网站?