当前位置:首页 > Windows程序 > 正文

如何用C#+WinRAR 实现压缩 分类:

2021-05-24 Windows程序

前提:必须安装 WinRAR

1. 工具类

[csharp] view plaincopy

技术分享

 

using System;  

using System.Diagnostics;  

using System.IO;  

using Microsoft.Win32;  

  

namespace Util  

{  

    public class RARClass  

    {  

        /// <summary>  

        /// 获取WinRAR.exe路径  

        /// </summary>  

        /// <returns>为空则表示未安装WinRAR</returns>  

        public static string ExistsRAR()  

        {  

            RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");  

            //RegistryKey regkey = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\shell\open\command");  

            string strkey = regkey.GetValue("").ToString();  

            regkey.Close();  

            //return strkey.Substring(1, strkey.Length - 7);  

            return strkey;  

        }  

  

        /// <summary>  

        /// 解压RAR文件  

        /// </summary>  

        /// <param name="rarFilePath">要解压的文件路径</param>  

        /// <param name="unrarDestPath">解压路径(绝对路径)</param>  

        public static void UnRAR(string rarFilePath, string unrarDestPath)  

        {  

            string rarexe = ExistsRAR();  

            if (String.IsNullOrEmpty(rarexe))  

            {  

                throw new Exception("未安装WinRAR程序。");  

            }  

            try  

            {  

                //组合出需要shell的完整格式  

                string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"", rarFilePath, unrarDestPath);  

  

                //用Process调用  

                using (Process unrar = new Process())  

                {  

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/70432.html