| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- 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);
- /// <summary>
- /// 释放内存
- /// </summary>
- public static void ClearMemory()
- {
- GC.Collect();
- GC.WaitForPendingFinalizers();
- //if (Environment.OSVersion.Platform == PlatformID.Win32NT)
- //{
- // SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1);
- //}
- }
- #endregion
- }
- }
|