helper.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using System;
  2. using System.Diagnostics;
  3. using System.Drawing;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. namespace ErpServer
  9. {
  10. public class helper
  11. {
  12. [DllImport("User32")]
  13. public static extern int SetForegroundWindow(int hwnd);
  14. [DllImport("User32.dll")]
  15. public static extern System.IntPtr FindWindow(string strclass, string strname);
  16. [DllImport("User32.dll")]
  17. public static extern System.IntPtr FindWindowEx(System.IntPtr parent, System.IntPtr childe, string strclass, string strname);
  18. [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
  19. public static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
  20. public const int SW_SHOWACTIVE = 4;
  21. private static string logPath = Application.StartupPath + "\\log";
  22. const int SW_HIDE = 0;
  23. #region
  24. private static object logFlag = new object();
  25. public static void writeLog(string log)
  26. {
  27. lock (logFlag)
  28. {
  29. using (FileStream fileStream = new FileStream(logPath + "\\" + DateTime.Now.ToString("yy-MM-dd") + ".log", FileMode.Append, FileAccess.Write))
  30. {
  31. using (StreamWriter sw = new StreamWriter(fileStream, Encoding.Default))
  32. {
  33. sw.Write(log + " ------时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
  34. sw.Flush();
  35. }
  36. }
  37. }
  38. }
  39. public static void clearLog()
  40. {
  41. //清除2天前的日志
  42. DateTime curTime = DateTime.Now.AddDays(-15);
  43. string[] files = Directory.GetFiles(logPath);
  44. foreach (string file in files)
  45. {
  46. string dt = Path.GetFileNameWithoutExtension(file);
  47. DateTime dTime = Convert.ToDateTime(dt);
  48. if (dTime.Subtract(curTime).Days < 0)
  49. {
  50. File.Delete(file);
  51. }
  52. }
  53. }
  54. public static void showLog()
  55. {
  56. string logFile = logPath + "\\" + DateTime.Now.ToString("yy-MM-dd") + ".log";
  57. if (logFile != "") Process.Start(logFile);
  58. else MessageBox.Show("没有日志文件可以查看!");
  59. }
  60. #endregion
  61. public static void ExecuteFile(string file)
  62. {
  63. Process p = new Process();
  64. p.StartInfo.FileName = file;
  65. p.StartInfo.CreateNoWindow = true;
  66. p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
  67. p.Start();
  68. p.WaitForExit(1000);
  69. p.Close();
  70. }
  71. public static void ExecuteFile1(string path, string file)
  72. {
  73. //声明一个程序信息类
  74. System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
  75. //设置外部程序名
  76. Info.FileName = file;
  77. //设置外部程序工作目录为 该应用程序的同级目录下(Debuge)
  78. Info.WorkingDirectory = path;
  79. //声明一个程序类
  80. System.Diagnostics.Process Proc;
  81. try
  82. {
  83. //启动外部程序
  84. Proc = System.Diagnostics.Process.Start(Info);
  85. }
  86. catch (System.ComponentModel.Win32Exception e)
  87. {
  88. helper.writeLog(string.Format("系统找不到指定的程序文件。\r{0}", e));
  89. return;
  90. }
  91. //打印出外部程序的开始执行时间
  92. helper.writeLog(string.Format("外部程序的开始执行时间:{0}", Proc.StartTime));
  93. //等待3秒钟
  94. Proc.WaitForExit(10000);
  95. //如果这个外部程序没有结束运行则对其强行终止
  96. if (!Proc.HasExited)
  97. {
  98. helper.writeLog("由主程序强行终止外部程序的运行!");
  99. Proc.Kill();
  100. }
  101. else
  102. {
  103. helper.writeLog("由外部程序正常退出!");
  104. }
  105. helper.writeLog(string.Format("外部程序的结束运行时间:{0}", Proc.ExitTime));
  106. helper.writeLog(string.Format("外部程序在结束运行时的返回值:{0}", Proc.ExitCode));
  107. }
  108. [DllImport("shell32.dll")]
  109. public extern static IntPtr ShellExecute(IntPtr hwnd,
  110. StringBuilder lpOperation,
  111. StringBuilder lpFile,
  112. StringBuilder lpParameters,
  113. StringBuilder lpDirectory,
  114. int nShowCmd
  115. );
  116. public enum ShowWindowCommands : int
  117. {
  118. SW_HIDE = 0,
  119. SW_SHOWNORMAL = 1,
  120. SW_NORMAL = 1,
  121. SW_SHOWMINIMIZED = 2,
  122. SW_SHOWMAXIMIZED = 3,
  123. SW_MAXIMIZE = 3,
  124. SW_SHOWNOACTIVATE = 4,
  125. SW_SHOW = 5,
  126. SW_MINIMIZE = 6,
  127. SW_SHOWMINNOACTIVE = 7,
  128. SW_SHOWNA = 8,
  129. SW_RESTORE = 9,
  130. SW_SHOWDEFAULT = 10,
  131. SW_MAX = 10
  132. }
  133. public static void SheelExe(IntPtr handle, string file, string path)
  134. {
  135. ShellExecute(handle, new StringBuilder("Open"), new StringBuilder(file), new StringBuilder(""), new StringBuilder(path), (int)ShowWindowCommands.SW_SHOWNORMAL);
  136. }
  137. //Base64加密
  138. public static string EncodingBase64(string str)
  139. {
  140. return EncodingBase64(str, Encoding.Default);
  141. }
  142. public static string EncodingBase64(string str, Encoding ens)
  143. {
  144. try
  145. {
  146. //str = EncryptSymmetric(str);
  147. return Convert.ToBase64String(ens.GetBytes(str));
  148. }
  149. catch
  150. {
  151. return str;
  152. }
  153. }
  154. //Base64解密
  155. public static string DecodingBase64(string str)
  156. {
  157. return DecodingBase64(str, Encoding.Default);
  158. }
  159. public static string DecodingBase64(string str, Encoding ens)
  160. {
  161. try
  162. {
  163. string result = ens.GetString(Convert.FromBase64String(str));
  164. //result = DecryptSymmetric(result);
  165. return result;
  166. }
  167. catch
  168. {
  169. return str;
  170. }
  171. }
  172. public static void AppendWithColor(RichTextBox rtb, string s_c)
  173. {
  174. AppendWithColor(rtb, s_c, Color.Black, null);
  175. }
  176. public static void AppendWithColor(RichTextBox rtb, string s_c, Color c)
  177. {
  178. AppendWithColor(rtb, s_c, c, null);
  179. }
  180. public static void AppendWithColor(RichTextBox rtb, string s_c, Color c, Font f)
  181. {
  182. rtb.AppendText(s_c);
  183. if (rtb.TextLength - s_c.Length < 0) rtb.SelectionStart = 0;
  184. else rtb.SelectionStart = rtb.TextLength - s_c.Length;
  185. rtb.SelectionLength = s_c.Length;
  186. if (c != null) rtb.SelectionColor = c;
  187. if (f != null) rtb.SelectionFont = f;
  188. }
  189. public static void AppendScroll(RichTextBox rtb)
  190. {
  191. rtb.Focus();
  192. rtb.Select(rtb.TextLength, 0);
  193. rtb.ScrollToCaret();
  194. }
  195. #region 内存回收
  196. [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")]
  197. public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize);
  198. /// <summary>
  199. /// 释放内存
  200. /// </summary>
  201. public static void ClearMemory()
  202. {
  203. GC.Collect();
  204. GC.WaitForPendingFinalizers();
  205. //if (Environment.OSVersion.Platform == PlatformID.Win32NT)
  206. //{
  207. // SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
  208. //}
  209. }
  210. #endregion
  211. }
  212. }