zoukankan      html  css  js  c++  java
  • 嵌入资源第三讲:多格式文件内嵌入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中文件的遍历及清理,使得使用过后不留痕迹,把活干得漂亮点^_^。

  • 相关阅读:
    HTML<head></head>中标签的含义
    axios发送post请求node服务器无法通过req.body获取参数
    Vue下URL地址栏参数改变却不能刷新界面
    Vue中img标签src属性绑定
    vue-cli 初始化项目时开发环境中的跨域问题
    【CODEVS2152】滑雪
    【NOIP2005】过河
    【CODEVS2776】寻找代表元
    【CodeVS1231】最优布线问题
    【CODEVS1073】家族
  • 原文地址:https://www.cnblogs.com/aswordok/p/3764780.html
Copyright © 2011-2022 走看看