using System; using System.Diagnostics; using System.Drawing; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Windows.Forms; namespace ErpServer { public class helper { [DllImport("User32")] public static extern int SetForegroundWindow(int hwnd); [DllImport("User32.dll")] public static extern System.IntPtr FindWindow(string strclass, string strname); [DllImport("User32.dll")] public static extern System.IntPtr FindWindowEx(System.IntPtr parent, System.IntPtr childe, string strclass, string strname); [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)] public static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow); public const int SW_SHOWACTIVE = 4; private static string logPath = Application.StartupPath + "\\log"; const int SW_HIDE = 0; #region private static object logFlag = new object(); public static void writeLog(string log) { lock (logFlag) { using (FileStream fileStream = new FileStream(logPath + "\\" + DateTime.Now.ToString("yy-MM-dd") + ".log", FileMode.Append, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fileStream, Encoding.Default)) { sw.Write(log + " ------时间:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n"); sw.Flush(); } } } } public static void clearLog() { //清除2天前的日志 DateTime curTime = DateTime.Now.AddDays(-15); string[] files = Directory.GetFiles(logPath); foreach (string file in files) { string dt = Path.GetFileNameWithoutExtension(file); DateTime dTime = Convert.ToDateTime(dt); if (dTime.Subtract(curTime).Days < 0) { File.Delete(file); } } } public static void showLog() { string logFile = logPath + "\\" + DateTime.Now.ToString("yy-MM-dd") + ".log"; if (logFile != "") Process.Start(logFile); else MessageBox.Show("没有日志文件可以查看!"); } #endregion public static void ExecuteFile(string file) { Process p = new Process(); p.StartInfo.FileName = file; p.StartInfo.CreateNoWindow = true; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.Start(); p.WaitForExit(1000); p.Close(); } public static void ExecuteFile1(string path, string file) { //声明一个程序信息类 System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo(); //设置外部程序名 Info.FileName = file; //设置外部程序工作目录为 该应用程序的同级目录下(Debuge) Info.WorkingDirectory = path; //声明一个程序类 System.Diagnostics.Process Proc; try { //启动外部程序 Proc = System.Diagnostics.Process.Start(Info); } catch (System.ComponentModel.Win32Exception e) { helper.writeLog(string.Format("系统找不到指定的程序文件。\r{0}", e)); return; } //打印出外部程序的开始执行时间 helper.writeLog(string.Format("外部程序的开始执行时间:{0}", Proc.StartTime)); //等待3秒钟 Proc.WaitForExit(10000); //如果这个外部程序没有结束运行则对其强行终止 if (!Proc.HasExited) { helper.writeLog("由主程序强行终止外部程序的运行!"); Proc.Kill(); } else { helper.writeLog("由外部程序正常退出!"); } helper.writeLog(string.Format("外部程序的结束运行时间:{0}", Proc.ExitTime)); helper.writeLog(string.Format("外部程序在结束运行时的返回值:{0}", Proc.ExitCode)); } [DllImport("shell32.dll")] public extern static IntPtr ShellExecute(IntPtr hwnd, StringBuilder lpOperation, StringBuilder lpFile, StringBuilder lpParameters, StringBuilder lpDirectory, int nShowCmd ); public enum ShowWindowCommands : int { SW_HIDE = 0, SW_SHOWNORMAL = 1, SW_NORMAL = 1, SW_SHOWMINIMIZED = 2, SW_SHOWMAXIMIZED = 3, SW_MAXIMIZE = 3, SW_SHOWNOACTIVATE = 4, SW_SHOW = 5, SW_MINIMIZE = 6, SW_SHOWMINNOACTIVE = 7, SW_SHOWNA = 8, SW_RESTORE = 9, SW_SHOWDEFAULT = 10, SW_MAX = 10 } public static void SheelExe(IntPtr handle, string file, string path) { ShellExecute(handle, new StringBuilder("Open"), new StringBuilder(file), new StringBuilder(""), new StringBuilder(path), (int)ShowWindowCommands.SW_SHOWNORMAL); } //Base64加密 public static string EncodingBase64(string str) { return EncodingBase64(str, Encoding.Default); } public static string EncodingBase64(string str, Encoding ens) { try { //str = EncryptSymmetric(str); return Convert.ToBase64String(ens.GetBytes(str)); } catch { return str; } } //Base64解密 public static string DecodingBase64(string str) { return DecodingBase64(str, Encoding.Default); } public static string DecodingBase64(string str, Encoding ens) { try { string result = ens.GetString(Convert.FromBase64String(str)); //result = DecryptSymmetric(result); return result; } catch { return str; } } public static void AppendWithColor(RichTextBox rtb, string s_c) { AppendWithColor(rtb, s_c, Color.Black, null); } public static void AppendWithColor(RichTextBox rtb, string s_c, Color c) { AppendWithColor(rtb, s_c, c, null); } public static void AppendWithColor(RichTextBox rtb, string s_c, Color c, Font f) { rtb.AppendText(s_c); if (rtb.TextLength - s_c.Length < 0) rtb.SelectionStart = 0; else rtb.SelectionStart = rtb.TextLength - s_c.Length; rtb.SelectionLength = s_c.Length; if (c != null) rtb.SelectionColor = c; if (f != null) rtb.SelectionFont = f; } public static void AppendScroll(RichTextBox rtb) { rtb.Focus(); rtb.Select(rtb.TextLength, 0); rtb.ScrollToCaret(); } #region 内存回收 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")] public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize); /// /// 释放内存 /// public static void ClearMemory() { GC.Collect(); GC.WaitForPendingFinalizers(); //if (Environment.OSVersion.Platform == PlatformID.Win32NT) //{ // SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); //} } #endregion } }