前提:必须安装 WinRAR
1. 工具类
- using System;
- using System.Diagnostics;
- using System.IO;
- using Microsoft.Win32;
-
- namespace Util
- {
- public class RARClass
- {
-
-
-
-
- public static string ExistsRAR()
- {
- RegistryKey regkey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\WinRAR.exe");
-
- string strkey = regkey.GetValue("").ToString();
- regkey.Close();
-
- return strkey;
- }
-
-
-
-
-
-
- public static void UnRAR(string rarFilePath, string unrarDestPath)
- {
- string rarexe = ExistsRAR();
- if (String.IsNullOrEmpty(rarexe))
- {
- throw new Exception("未安装WinRAR程序。");
- }
- try
- {
-
- string shellArguments = string.Format("x -o+ \"{0}\" \"{1}\\\"", rarFilePath, unrarDestPath);
-
-
- using (Process unrar = new Process())
- {
- ProcessStartInfo startinfo = new ProcessStartInfo();
- startinfo.FileName = rarexe;
- startinfo.Arguments = shellArguments;
- startinfo.WindowStyle = ProcessWindowStyle.Hidden;
-
- unrar.StartInfo = startinfo;
- unrar.Start();
- unrar.WaitForExit();
-
- unrar.Close();
- }
- }
- catch
- {
- throw;
- }
- }
-
-
-
-
-
-
- public static void RAR(string filePath, string rarfilePath, string otherPara )
- {
- RAR(filePath, rarfilePath, "", "", otherPara);
- }
-
-
-
-
-
-
-
- public static void RAR(string filePath, string rarfilePath, string rarName, string otherPara)
- {
- RAR(filePath, rarfilePath, rarName, "", otherPara);
- }
-
-
-
-
-
-
-
-
- public static void RAR(string filePath, string rarfilePath, string rarName, string password, string otherPara)
- {
- string rarexe = ExistsRAR();
- if (String.IsNullOrEmpty(rarexe))
- {
- throw new Exception("未安装WinRAR程序。");
- }
-
- if (!Directory.Exists(filePath))
- {
-
- }
-
- if (String.IsNullOrEmpty(rarName))
- {
- rarName = Path.GetFileNameWithoutExtension(filePath) + ".rar";
- }
- else
- {
- if (Path.GetExtension(rarName).ToLower() != ".rar")
- {
- rarName += ".rar";
- }
- }
-
- try
- {
-
-
- string shellArguments;
- if (String.IsNullOrEmpty(password))
- {
- shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r", rarName, filePath);
- }
- else
- {
- shellArguments = string.Format("a -ep1 \"{0}\" \"{1}\" -r -p\"{2}\"", rarName, filePath, password);
- }
- if (!string.IsNullOrEmpty(otherPara))
- {
- shellArguments = shellArguments + " " + otherPara;
- }
-
- using (Process rar = new Process())
- {
- ProcessStartInfo startinfo = new ProcessStartInfo();
- startinfo.FileName = rarexe;
- startinfo.Arguments = shellArguments;
- startinfo.WindowStyle = ProcessWindowStyle.Hidden;
- startinfo.WorkingDirectory = rarfilePath;
-
- rar.StartInfo = startinfo;
- rar.Start();
- rar.WaitForExit();
- rar.Close();
- }
- }
- catch
- {
- throw;
- }
- }
-
- }
- }
2. 测试程序
- using System;
- using Util;
-
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- string path = "d:\\data.txt";
- string rarPath = "d:\\";
- string rarName = "";
-
- RARClass.RAR(path, rarPath, rarName, "-agYYYYMMDD -ibck");
- Console.WriteLine("End");
- Console.Read();
- }
- }
- }
如何用C#+WinRAR 实现压缩 分类:
原文:http://www.cnblogs.com/just09161018/p/4605294.html