WinrarHelper.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using Microsoft.Win32;
  2. using System.Diagnostics;
  3. namespace Masuit.Tools.Files
  4. {
  5. /// <summary>
  6. /// WinRAR压缩操作
  7. /// </summary>
  8. public static class WinrarHelper
  9. {
  10. #region 压缩
  11. /// <summary>
  12. /// 压缩
  13. /// </summary>
  14. /// <param name="zipname">要解压的文件名</param>
  15. /// <param name="zippath">要压缩的文件目录</param>
  16. /// <param name="dirpath">初始目录</param>
  17. public static void Rar(string zipname, string zippath, string dirpath)
  18. {
  19. _theReg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRAR.exe\Shell\Open\Command");
  20. if (_theReg != null)
  21. {
  22. _theObj = _theReg.GetValue("");
  23. _theRar = _theObj.ToString();
  24. _theReg?.Close();
  25. }
  26. _theRar = _theRar.Substring(1, _theRar.Length - 7);
  27. _theInfo = " a " + zipname + " " + zippath;
  28. _theStartInfo = new ProcessStartInfo
  29. {
  30. FileName = _theRar,
  31. Arguments = _theInfo,
  32. WindowStyle = ProcessWindowStyle.Hidden,
  33. WorkingDirectory = dirpath
  34. };
  35. _theProcess = new Process
  36. {
  37. StartInfo = _theStartInfo
  38. };
  39. _theProcess.Start();
  40. }
  41. #endregion
  42. #region 解压缩
  43. /// <summary>
  44. /// 解压缩
  45. /// </summary>
  46. /// <param name="zipname">要解压的文件名</param>
  47. /// <param name="zippath">要解压的文件路径</param>
  48. public static void UnRar(string zipname, string zippath)
  49. {
  50. _theReg = Registry.ClassesRoot.OpenSubKey(@"Applications\WinRar.exe\Shell\Open\Command");
  51. if (_theReg != null)
  52. {
  53. _theObj = _theReg.GetValue("");
  54. _theRar = _theObj.ToString();
  55. _theReg.Close();
  56. }
  57. _theRar = _theRar.Substring(1, _theRar.Length - 7);
  58. _theInfo = " X " + zipname + " " + zippath;
  59. _theStartInfo = new ProcessStartInfo
  60. {
  61. FileName = _theRar,
  62. Arguments = _theInfo,
  63. WindowStyle = ProcessWindowStyle.Hidden
  64. };
  65. _theProcess = new Process
  66. {
  67. StartInfo = _theStartInfo
  68. };
  69. _theProcess.Start();
  70. }
  71. #endregion
  72. #region 私有变量
  73. private static string _theRar;
  74. private static RegistryKey _theReg;
  75. private static object _theObj;
  76. private static string _theInfo;
  77. private static ProcessStartInfo _theStartInfo;
  78. private static Process _theProcess;
  79. #endregion
  80. }
  81. }