CommonFunctions.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Text;
  6. namespace ZipFileHelper
  7. {
  8. class CommonFunctions
  9. {
  10. #region 单例模式
  11. private static CommonFunctions uniqueInstance;
  12. private static object _lock = new object();
  13. private CommonFunctions() { }
  14. public static CommonFunctions getInstance()
  15. {
  16. if (null == uniqueInstance) //确认要实例化后再进行加锁,降低加锁的性能消耗。
  17. {
  18. lock (_lock)
  19. {
  20. if (null == uniqueInstance)
  21. {
  22. uniqueInstance = new CommonFunctions();
  23. }
  24. }
  25. }
  26. return uniqueInstance;
  27. }
  28. #endregion
  29. #region 进程
  30. /// <summary>
  31. /// 另起一进程执行命令
  32. /// </summary>
  33. /// <param name="exe">可执行程序(路径+名)</param>
  34. /// <param name="commandInfo">执行命令</param>
  35. /// <param name="workingDir">执行所在初始目录</param>
  36. /// <param name="processWindowStyle">进程窗口样式</param>
  37. /// <param name="isUseShellExe">Shell启动程序</param>
  38. public void ExecuteProcess(string exe, string commandInfo, string workingDir = "", ProcessWindowStyle processWindowStyle = ProcessWindowStyle.Hidden, bool isUseShellExe = false)
  39. {
  40. ProcessStartInfo startInfo = new ProcessStartInfo();
  41. startInfo.FileName = exe;
  42. startInfo.Arguments = commandInfo;
  43. startInfo.WindowStyle = processWindowStyle;
  44. startInfo.UseShellExecute = isUseShellExe;
  45. if (!string.IsNullOrWhiteSpace(workingDir))
  46. {
  47. startInfo.WorkingDirectory = workingDir;
  48. }
  49. ExecuteProcess(startInfo);
  50. }
  51. /// <summary>
  52. /// 直接另启动一个进程
  53. /// </summary>
  54. /// <param name="startInfo">启动进程时使用的一组值</param>
  55. public void ExecuteProcess(ProcessStartInfo startInfo)
  56. {
  57. try
  58. {
  59. Process process = new Process();
  60. process.StartInfo = startInfo;
  61. process.Start();
  62. process.WaitForExit();
  63. process.Close();
  64. process.Dispose();
  65. }
  66. catch (Exception ex)
  67. {
  68. throw new Exception("进程执行失败:\n\r" + startInfo.FileName + "\n\r" + ex.Message);
  69. }
  70. }
  71. /// <summary>
  72. /// 去掉文件夹或文件的只读属性
  73. /// </summary>
  74. /// <param name="objectPathName">文件夹或文件全路径</param>
  75. public void Attribute2Normal(string objectPathName)
  76. {
  77. if (true == Directory.Exists(objectPathName))
  78. {
  79. System.IO.DirectoryInfo directoryInfo = new System.IO.DirectoryInfo(objectPathName);
  80. directoryInfo.Attributes = FileAttributes.Normal;
  81. }
  82. else
  83. {
  84. File.SetAttributes(objectPathName, FileAttributes.Normal);
  85. }
  86. }
  87. #endregion
  88. }
  89. }