access_time 13 years ago language中文
more_vert

.net C# winrar 解压程序

visibility 462 comment 0
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using Microsoft.Win32;

namespace SmartOfflineUpdater
{
    /// <summary>
    /// 解压帮助类
    /// </summary>
    public class UnPackHelper
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="filePath">解压文件路径</param>
        /// <param name="folder">解压到文件夹</param>
        public UnPackHelper(string filePath, string folder)
        {
            FilePath = filePath;
            UnPackDirectory = folder;
        }

        private string m_FilePath;

        /// <summary>
        /// 解压文件路径
        /// </summary>
        public string FilePath
        {
            get { return m_FilePath; }
            set { m_FilePath = value; }
        }

        private string m_UnPackDirectory;

        /// <summary>
        /// 解压到文件夹路径
        /// </summary>
        public string UnPackDirectory
        {
            get { return m_UnPackDirectory; }
            set { m_UnPackDirectory = value; }
        }

        /// <summary>
        /// 解压开始
        /// </summary>
        public void DoUnPack()
        {
            Console.WriteLine("开始解压......");
            RegistryKey reg;
            string rarPath;
            Process process = new Process();

            try
            {
                reg=Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
                rarPath = reg.GetValue("").ToString();
                rarPath = rarPath.Substring(1, rarPath.Length - 7);
                process.StartInfo.FileName = rarPath;
                process.StartInfo.CreateNoWindow = true;
                process.StartInfo.UseShellExecute = false;
                // -0+ 如果已经存在则覆盖
                process.StartInfo.Arguments = string.Format(" x -o+ {0} {1}", FilePath, UnPackDirectory);//这里是命令行 请参考winrar的说明
                //开始解压
                process.Start();
                //进程终止
                if (process.HasExited)
                {
                    int exitCode = process.ExitCode;
                    if (exitCode == 0)
                        Console.WriteLine("{0} 正常完成", exitCode.ToString());
                    else
                        Console.WriteLine("{0} 正常完成", exitCode.ToString());
                }
                Console.WriteLine("解压完成");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //释放资源
                process.Close();
            }
        }

        /// <summary>
        /// 外部调用解压
        /// </summary>
        /// <param name="filePath"></param>
        /// <param name="folder"></param>
        public static void DoUnPack(string filePath, string folder)
        {
            UnPackHelper helper = new UnPackHelper(filePath, folder);
            helper.DoUnPack();
        }
    }
}
info Last modified by Raymond 13 years ago copyright This page is subject to Site terms.
Like this article?
Share on

Please log in or register to comment.

account_circle Log in person_add Register

Log in with external accounts

Follow Kontext

Get our latest updates on LinkedIn.

Want to contribute on Kontext to help others?

Learn more

More from Kontext

visibility 34
thumb_up 0
access_time 8 months ago
visibility 3214
thumb_up 0
access_time 4 years ago