刚写了个winform的安装程序,在此做下记录

导言

刚写了个winform的安装程序,在此做下记录

1. 及时处理

挺常用的,以下来自引用:

因为winform的消息循环是一个线程来处理,那么假如你的某个操作比较耗时,那么消息处理得等你这个耗时操作做完了才能继续,而Application.DoEvents方法就是允许你在耗时操作的内部调用它,而去处理消息队列中的消息。

Application.DoEvents

2. 资源嵌入和使用

因为我想着最后生成的是一个大exe,里面用到的包通过嵌入资源的方式都生成到exe中了(exe的文件会很大),用的时候再拿出来

嵌入资源

用的时候通过读取资源文件流,保存到临时文件,安装完成后再删除

        //获得正在运行类所在的名称空间
        Type type = MethodBase.GetCurrentMethod().DeclaringType;
        string _namespace = type.Namespace;
        //临时文件
        string folderPath = Application.StartupPath + @"\临时文件res";
        Directory.CreateDirectory(folderPath);
        //获得当前运行的Assembly
        Assembly _assembly = Assembly.GetExecutingAssembly();

        //文件名
        string name = "activemq.zip";
        //根据名称空间和文件名生成资源名称
        string resourceName = _namespace + ".res."+ name;
        //根据资源名称从Assembly中获取此资源的Stream
        Stream stream = _assembly.GetManifestResourceStream(resourceName);
        //全部的资源名
        //string[] resNames = _assembly.GetManifestResourceNames();
        byte[] srcBuf = new Byte[stream.Length];
        stream.Read(srcBuf, 0, srcBuf.Length);
        stream.Seek(0, SeekOrigin.Begin);
        //保存文件到本地
        try
        {
            using (FileStream fs = new FileStream(folderPath+ @"\"+ name, FileMode.OpenOrCreate, FileAccess.Write))
            {
                fs.Write(srcBuf, 0, srcBuf.Length);
                fs.Close();
            }
        }
        catch (Exception ex)
        {
        }

删除

DirectoryInfo subdir = new DirectoryInfo(Application.StartupPath + @"\临时文件res");
subdir.Delete(true);          //删除子目录和文件

3. 解压

jar包利用代码解压后文件损坏了……,网上找了好几个,终于有一个是可以用的,可以做到无损。还是用的 ICSharpCode.SharpZipLib.dll 也作为资源嵌入了

    ZipHelper.UnpackFiles(Application.StartupPath + "\\临时文件res\\activemq.zip", @"c:\MQ");//解压

    jar uf更新
    
    jar xf解压
    /// <summary>
    /// 解压缩文件
    /// </summary>
    /// <param name="zipFile">待解压缩的文件</param>
    /// <param name="directory">解压缩后文件存放的目录</param>
    public static bool UnpackFiles(string zipFile, string directory)
    {
        if (!Directory.Exists(directory))
            Directory.CreateDirectory(directory);

        ZipInputStream stream = new ZipInputStream(File.OpenRead(zipFile));
        ZipEntry theEntry = null;
        while ((theEntry = stream.GetNextEntry()) != null)
        {
            string directoryName = Path.GetDirectoryName(theEntry.Name);
            string fileName = Path.GetFileName(theEntry.Name);
            if (directoryName != string.Empty)
                Directory.CreateDirectory(directory + "\\" + directoryName);

            if (fileName != string.Empty)
            {
                FileStream streamWriter = File.Create(Path.Combine(directory, theEntry.Name));
                int size = 2048;
                byte[] data = new byte[size];
                while (true)
                {
                    size = stream.Read(data, 0, data.Length);
                    if (size > 0)
                        streamWriter.Write(data, 0, size);
                    else
                        break;
                }
                streamWriter.Close();
            }
        }
        stream.Close();
        return true;
    }

4. 运行bat

启动MQ时候直接运行的bat

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.WorkingDirectory = @"c:\MQ\bin\win64"; 
    proc.StartInfo.FileName = "activemq.bat";
    proc.StartInfo.Arguments = string.Format("10");//this is argument
    proc.StartInfo.CreateNoWindow = false;
    //proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;//这里设置DOS窗口不显示,经实践可行
    proc.Start();