我正在开发一个 3D 应用程序,用户可以在其中加载多个图像,我使用的 3D 库具有将整个场景导出为 XAML 的功能,当然在 XAML 中我只有图像的路径,所以文件只在创建它的机器上有效,我想做的是保存 XAML,将 XAML 中的图像路径更改为相对于 XAML 文件路径,保存图像XAML 在同一个新文件中,这样新文件将始终包含 XAML 和图像。如何在同一文件中保存多种文件类型?对整个想法的任何其他建议将不胜感激。
最佳答案
如果您使用的是 .NET 4.5,则可以使用新的 ZipFile
class 以编程方式将多个文件轻松保存到一个 zip 文件夹中.
首先,您需要将文件准备到一个新文件夹(Directory.CreateDirectory
、File.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/