嵌入资源第三讲:多格式文件内嵌入WPF资源文件

作为一个扩展,你需要了解DotNetZip用法,请参见:
C# .NET 使用第三方类库DotNetZip解压/压缩Zip文件

你也需要了解单文件内嵌入资源文件基本方法,参见:
WPF调用嵌入的非.net的EXE资源文件

作者:一剑

如果你有一大堆文件或者想通过打包的方式嵌入任意格式的文件到资源文件中,那么你可以打包成一个ZIP文件,再嵌入到资源文件中是一个不错的选择:

using Ionic.Zip;//+
using System.Reflection;
using System.IO;
using System.Threading;

namespace WpfApplication2
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        List<string> fileList = new List<string>();
        public MainWindow()
        {
            InitializeComponent();

            Thread t = new Thread(new ThreadStart(unzip));
            t.IsBackground = true;
            t.Start();
        }

        private void unzip()
        {
            String projectName = Assembly.GetExecutingAssembly().GetName().Name.ToString();
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(projectName + ".f.zip"))
            {
                Byte[] b = new Byte[stream.Length];
                stream.Read(b, 0, b.Length);
                MemoryStream m = new MemoryStream(b);
                using (ZipFile zip = ZipFile.Read(m))
                {
                    zip.ExtractAll(System.IO.Path.GetTempPath(), ExtractExistingFileAction.OverwriteSilently);
                    foreach (ZipEntry entry in zip)
                    {
                        fileList.Add(System.IO.Path.GetTempPath() + entry.FileName);
                    }
                }
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            foreach (string f in fileList)
            {
                if (File.Exists(f))
                    MessageBox.Show(f + " is found.");
            }
        }
private void Window_Closed(object sender, EventArgs e) {foreach (string f in fileList) { if (File.Exists(f)) File.Delete(f); } } } }

 

在这篇示例中,在程序结束时加入了ZIP中文件的遍历及清理,使得使用过后不留痕迹,把活干得漂亮点^_^。

转载于:https://www.cnblogs.com/aswordok/p/3764780.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值