| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Management;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Windows.Forms;
- namespace ErpServer
- {
- public partial class ServerForm : Form
- {
- private static readonly string iniFile = Application.StartupPath + "\\config.ini";
- private static readonly string logPath = Application.StartupPath + "\\log";
- public ServerForm()
- {
- InitializeComponent();
- }
- #region hideform
- [DllImport("user32.dll")]
- public static extern Int32 GetWindowLong(IntPtr hwnd, Int32 index);
- [DllImport("user32.dll")]
- public static extern Int32 SetWindowLong(IntPtr hwnd, Int32 index, Int32 newValue);
- public const int GWL_EXSTYLE = (-20);
- private static void AddWindowExStyle(IntPtr hwnd, Int32 val)
- {
- int oldValue = GetWindowLong(hwnd, GWL_EXSTYLE);
- if (oldValue == 0)
- {
- throw new System.ComponentModel.Win32Exception();
- }
- if (0 == SetWindowLong(hwnd, GWL_EXSTYLE, oldValue | val))
- {
- throw new System.ComponentModel.Win32Exception();
- }
- }
- public static int WS_EX_TOOLWINDOW = 0x00000080;
- //我把这个过程封装下:
- public static void SetFormToolWindowStyle(Form form)
- {
- AddWindowExStyle(form.Handle, WS_EX_TOOLWINDOW);
- }
- #endregion
- string runTime = "";
- string siteUrl = "";
- HttpHelper http = null;
- HttpResult hResult = null;
- Thread runTh = null;
- private void ServerForm_Load(object sender, EventArgs e)
- {
- SetFormToolWindowStyle(this);
- CheckForIllegalCrossThreadCalls = false;
- http = new HttpHelper();
- if (!Directory.Exists(logPath)) Directory.CreateDirectory(logPath);
- string ofolder = INI.GetIniValue("SET", "OFolder", iniFile);
- if (ofolder.Length > 0) txtFolder.Text = ofolder;
- string nfolder = INI.GetIniValue("SET", "NFolder", iniFile);
- if (nfolder.Length > 0) txtActionFolder.Text = nfolder;
- runTime = INI.GetIniValue("SET", "RunTime", iniFile);
- siteUrl = INI.GetIniValue("SET", "SiteUrl", iniFile);
- helper.clearLog();
- serverTimer.Enabled = true;
- serverTimer.Interval = 1000 * 58;
- serverTimer.Tick += ServerTimer_Tick;
- serverTimer.Start();
- }
- bool isRun = false;
- private void ServerTimer_Tick(object sender, EventArgs e)
- {
- if(DateTime.Now.ToString("HH:mm")==runTime)
- {
- if (isRun) return;
- isRun = true;
- runTh = new Thread(new ThreadStart(setRunThread));
- runTh.Start();
- }
- }
- private void setRunThread()
- {
- try
- {
- if (!killProc("lyxg")) return;
- Thread.Sleep(1000);
- HttpItem item = new HttpItem()
- {
- URL = siteUrl
- };
- hResult = http.GetHtml(item);
- helper.writeLog("重启w3wp.exe并访问网站");
- }
- catch (Exception ex)
- {
- helper.writeLog(ex.Message);
- }
- finally
- {
- isRun = false;
- }
- }
- //结束进程
- private bool killProc(string user)
- {
- Process[] pProcess = Process.GetProcesses();
- for (int i = 1; i <= pProcess.Length - 1; i++)
- {
- //helper.writeLog(pProcess[i].ProcessName.ToLower() + "," + GetProcessUserName(pProcess[i].Id));
- if (pProcess[i].ProcessName.ToLower() == "w3wp" && GetProcessUserName(pProcess[i].Id) == user)
- {
- pProcess[i].Kill();
- return true;
- }
- }
- return false;
- }
- //获得进程的用户名
- private static string GetProcessUserName(int pID)
- {
- string text1 = null;
- SelectQuery query1 =new SelectQuery("Select * from Win32_Process WHERE processID=" + pID);
- ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(query1);
- try
- {
- foreach (ManagementObject disk in searcher1.Get())
- {
- ManagementBaseObject inPar = null;
- ManagementBaseObject outPar = null;
- inPar = disk.GetMethodParameters("GetOwner");
- outPar = disk.InvokeMethod("GetOwner", inPar, null);
- text1 = outPar["User"].ToString();
- break;
- }
- }
- catch
- {
- text1 = "SYSTEM";
- }
- return text1;
- }
- Thread th = null;
- private void btnSure_Click(object sender, EventArgs e)
- {
- btnSure.Enabled = false;
- th = new Thread(new ThreadStart(updateThread));
- th.Start();
- }
- private void updateThread()
- {
- txtCon.Clear();
- try
- {
- string folder = Application.StartupPath + "\\" + txtFolder.Text;
- string target = txtActionFolder.Text;
- if (!Directory.Exists(target))
- {
- AppendLog("找不到目标路径...执行结束!");
- return;
- }
- if (!Directory.Exists(folder))
- {
- AppendLog("找不到更新目录...执行结束!");
- return;
- }
- AppendLog("开始更新站点...");
- AppendLog("结束w3wp.exe进程...");
- killProc("lyxg");
- AppendLog("进程己结束...");
- Thread.Sleep(500);
- AppendLog("删除目标路径的BIN目录...");
- if (Directory.Exists(target + "\\bin")) Directory.Delete(target + "\\bin", true);
- AppendLog("覆盖其它所有文件目录...");
- DirectoryCopy(folder, target, true);
- Thread.Sleep(500);
- AppendLog("执行完成!!");
- }
- catch (Exception ex)
- {
- AppendLog("发生错误:" + ex.Message);
- }
- finally
- {
- btnSure.Enabled = true;
- }
- }
- private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
- {
- DirectoryInfo dir = new DirectoryInfo(sourceDirName);
- DirectoryInfo[] dirs = dir.GetDirectories();
- if (!dir.Exists)
- {
- throw new DirectoryNotFoundException(
- "Source directory does not exist or could not be found: "
- + sourceDirName);
- }
- if (!Directory.Exists(destDirName))
- {
- Directory.CreateDirectory(destDirName);
- }
- FileInfo[] files = dir.GetFiles();
- foreach (FileInfo file in files)
- {
- string temppath = Path.Combine(destDirName, file.Name);
- file.CopyTo(temppath, true);
- }
- if (copySubDirs)
- {
- foreach (DirectoryInfo subdir in dirs)
- {
- string temppath = Path.Combine(destDirName, subdir.Name);
- DirectoryCopy(subdir.FullName, temppath, copySubDirs);
- }
- }
- }
- public void AppendLog(string msg)
- {
- AppendLog(msg, Color.Blue);
- }
- public void AppendLog(string msg, Color c)
- {
- AppendLog(txtCon, "", msg, c);
- }
- public void AppendFailLog(string msg, Color c)
- {
- AppendLog(txtCon, "", msg, c);
- }
- public void AppendLog(RichTextBox rtb, string tag, string msg, Color c)
- {
- try
- {
- string txt = msg + "\r\n";
- if (this.IsHandleCreated)
- {
- this.Invoke(new MethodInvoker(delegate
- {
- //RemovePrevAppend(rtb);
- AppendWithColor(rtb, txt, c);
- rtb.SelectionStart = rtb.TextLength;
- rtb.ScrollToCaret();
- }));
- }
- else
- {
- //RemovePrevAppend(rtb);
- AppendWithColor(rtb, txt, c);
- rtb.SelectionStart = rtb.TextLength;
- rtb.ScrollToCaret();
- }
- }
- catch (Exception ex)
- {
- helper.writeLog("添加RICHTEXTBOX内容时发生错误:" + ex.Message);
- }
- }
- private void RemovePrevAppend(RichTextBox rtb)
- {
- if (rtb.Lines.Length >= 500)
- {
- int len = rtb.Lines.Length;
- string[] tempArray = rtb.Lines;
- string[] sNewLines = new string[100];
- Array.Copy(tempArray, len - 101, sNewLines, 0, 100);
- rtb.Lines = sNewLines;
- }
- }
- public string getDateTime()
- {
- return "[" + DateTime.Now.ToString("HH:mm:ss") + "]";
- }
- 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;
- }
- private void stripSetting_Click(object sender, EventArgs e)
- {
- WindowState = FormWindowState.Normal;
- ShowInTaskbar = true;
- this.Show();
- }
- private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (e.CloseReason == CloseReason.UserClosing)
- {
- INI.SetIniValue("SET", "OFolder", txtFolder.Text, iniFile);
- INI.SetIniValue("SET", "NFolder", txtActionFolder.Text, iniFile);
- // 取消关闭
- e.Cancel = true;
- this.Hide();
- ShowInTaskbar = false;
- }
- }
- private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
- {
- helper.writeLog("客户端关闭");
- }
- private void notifyIcon_DoubleClick(object sender, EventArgs e)
- {
- WindowState = FormWindowState.Normal;
- ShowInTaskbar = true;
- this.Show();
- }
- private void stripExit_Click(object sender, EventArgs e)
- {
- Application.ExitThread();
- Application.Exit();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- setRunThread();
- }
- }
- }
|