winRAR.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /******************************************************************
  2. ** 文 件 名: winRAR.cs
  3. ** 创 建 人: wufs
  4. ** 创建日期: 2009/12/11 15:29:17
  5. ** 描 述: 使用winRar来对文件进行压缩处理
  6. ** 版 本: 1.0.0
  7. ** 修改描述:
  8. ** 修 改 人:
  9. ** 修改日期:
  10. ******************************************************************/
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Text;
  15. using System.IO;
  16. using System.Diagnostics;
  17. using System.Configuration;
  18. namespace Bowin.Common
  19. {
  20. public class WinRAR
  21. {
  22. private static string _RarPath = ConfigurationManager.AppSettings["WinRARPath"];
  23. /// <summary>
  24. /// 把一个文件压缩(文件源删除)
  25. /// </summary>
  26. /// <param name="sourceFile"></param>
  27. /// <param name="RarFile"></param>
  28. /// <returns></returns>
  29. public static bool Rar(string sourceFile,string RarFile)
  30. {
  31. string DOSstr = "m -ep1 \"{0}\" \"{1}\"";
  32. DOSstr = string.Format(DOSstr,RarFile,sourceFile);
  33. return doRar(DOSstr);
  34. }
  35. /// <summary>
  36. /// 把一个文件按相对路径解压
  37. /// </summary>
  38. /// <param name="sourceFile"></param>
  39. /// <param name="unUrlPath"></param>
  40. /// <returns></returns>
  41. public static bool unRar(string sourceFile,string unUrlPath)
  42. {
  43. string DOSstr = "x \"{0}\" \"{1}\"";
  44. DOSstr = string.Format(DOSstr, sourceFile, unUrlPath);
  45. return doRar(DOSstr);
  46. }
  47. /// <summary>
  48. /// 接收操作winRar的命令
  49. /// eg.->Rar a file.rar file.txt
  50. /// 传入字符串:
  51. /// ->"a file.rar file.txt"
  52. /// </summary>
  53. /// <param name="DOSstr"></param>
  54. /// <returns></returns>
  55. public static bool doRar(string DOSstr)
  56. {
  57. bool ret = false;
  58. Process p = new Process();
  59. p.StartInfo.FileName = _RarPath;
  60. p.StartInfo.UseShellExecute = true;
  61. p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  62. try
  63. {
  64. p.StartInfo.Arguments=DOSstr;
  65. p.Start();
  66. p.WaitForExit();
  67. p.Close();
  68. ret=true;
  69. }
  70. catch
  71. {
  72. p.Close();
  73. }
  74. return ret;
  75. }
  76. }
  77. }