ServerForm.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Diagnostics;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Management;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. using System.Threading;
  12. using System.Windows.Forms;
  13. namespace ErpServer
  14. {
  15. public partial class ServerForm : Form
  16. {
  17. private static readonly string iniFile = Application.StartupPath + "\\config.ini";
  18. private static readonly string logPath = Application.StartupPath + "\\log";
  19. public ServerForm()
  20. {
  21. InitializeComponent();
  22. }
  23. #region hideform
  24. [DllImport("user32.dll")]
  25. public static extern Int32 GetWindowLong(IntPtr hwnd, Int32 index);
  26. [DllImport("user32.dll")]
  27. public static extern Int32 SetWindowLong(IntPtr hwnd, Int32 index, Int32 newValue);
  28. public const int GWL_EXSTYLE = (-20);
  29. private static void AddWindowExStyle(IntPtr hwnd, Int32 val)
  30. {
  31. int oldValue = GetWindowLong(hwnd, GWL_EXSTYLE);
  32. if (oldValue == 0)
  33. {
  34. throw new System.ComponentModel.Win32Exception();
  35. }
  36. if (0 == SetWindowLong(hwnd, GWL_EXSTYLE, oldValue | val))
  37. {
  38. throw new System.ComponentModel.Win32Exception();
  39. }
  40. }
  41. public static int WS_EX_TOOLWINDOW = 0x00000080;
  42. //我把这个过程封装下:
  43. public static void SetFormToolWindowStyle(Form form)
  44. {
  45. AddWindowExStyle(form.Handle, WS_EX_TOOLWINDOW);
  46. }
  47. #endregion
  48. string runTime = "";
  49. string siteUrl = "";
  50. HttpHelper http = null;
  51. HttpResult hResult = null;
  52. Thread runTh = null;
  53. private void ServerForm_Load(object sender, EventArgs e)
  54. {
  55. SetFormToolWindowStyle(this);
  56. CheckForIllegalCrossThreadCalls = false;
  57. http = new HttpHelper();
  58. if (!Directory.Exists(logPath)) Directory.CreateDirectory(logPath);
  59. string ofolder = INI.GetIniValue("SET", "OFolder", iniFile);
  60. if (ofolder.Length > 0) txtFolder.Text = ofolder;
  61. string nfolder = INI.GetIniValue("SET", "NFolder", iniFile);
  62. if (nfolder.Length > 0) txtActionFolder.Text = nfolder;
  63. runTime = INI.GetIniValue("SET", "RunTime", iniFile);
  64. siteUrl = INI.GetIniValue("SET", "SiteUrl", iniFile);
  65. helper.clearLog();
  66. serverTimer.Enabled = true;
  67. serverTimer.Interval = 1000 * 58;
  68. serverTimer.Tick += ServerTimer_Tick;
  69. serverTimer.Start();
  70. }
  71. bool isRun = false;
  72. private void ServerTimer_Tick(object sender, EventArgs e)
  73. {
  74. if(DateTime.Now.ToString("HH:mm")==runTime)
  75. {
  76. if (isRun) return;
  77. isRun = true;
  78. runTh = new Thread(new ThreadStart(setRunThread));
  79. runTh.Start();
  80. }
  81. }
  82. private void setRunThread()
  83. {
  84. try
  85. {
  86. if (!killProc("lyxg")) return;
  87. Thread.Sleep(1000);
  88. HttpItem item = new HttpItem()
  89. {
  90. URL = siteUrl
  91. };
  92. hResult = http.GetHtml(item);
  93. helper.writeLog("重启w3wp.exe并访问网站");
  94. }
  95. catch (Exception ex)
  96. {
  97. helper.writeLog(ex.Message);
  98. }
  99. finally
  100. {
  101. isRun = false;
  102. }
  103. }
  104. //结束进程
  105. private bool killProc(string user)
  106. {
  107. Process[] pProcess = Process.GetProcesses();
  108. for (int i = 1; i <= pProcess.Length - 1; i++)
  109. {
  110. //helper.writeLog(pProcess[i].ProcessName.ToLower() + "," + GetProcessUserName(pProcess[i].Id));
  111. if (pProcess[i].ProcessName.ToLower() == "w3wp" && GetProcessUserName(pProcess[i].Id) == user)
  112. {
  113. pProcess[i].Kill();
  114. return true;
  115. }
  116. }
  117. return false;
  118. }
  119. //获得进程的用户名
  120. private static string GetProcessUserName(int pID)
  121. {
  122. string text1 = null;
  123. SelectQuery query1 =new SelectQuery("Select * from Win32_Process WHERE processID=" + pID);
  124. ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(query1);
  125. try
  126. {
  127. foreach (ManagementObject disk in searcher1.Get())
  128. {
  129. ManagementBaseObject inPar = null;
  130. ManagementBaseObject outPar = null;
  131. inPar = disk.GetMethodParameters("GetOwner");
  132. outPar = disk.InvokeMethod("GetOwner", inPar, null);
  133. text1 = outPar["User"].ToString();
  134. break;
  135. }
  136. }
  137. catch
  138. {
  139. text1 = "SYSTEM";
  140. }
  141. return text1;
  142. }
  143. Thread th = null;
  144. private void btnSure_Click(object sender, EventArgs e)
  145. {
  146. btnSure.Enabled = false;
  147. th = new Thread(new ThreadStart(updateThread));
  148. th.Start();
  149. }
  150. private void updateThread()
  151. {
  152. txtCon.Clear();
  153. try
  154. {
  155. string folder = Application.StartupPath + "\\" + txtFolder.Text;
  156. string target = txtActionFolder.Text;
  157. if (!Directory.Exists(target))
  158. {
  159. AppendLog("找不到目标路径...执行结束!");
  160. return;
  161. }
  162. if (!Directory.Exists(folder))
  163. {
  164. AppendLog("找不到更新目录...执行结束!");
  165. return;
  166. }
  167. AppendLog("开始更新站点...");
  168. AppendLog("结束w3wp.exe进程...");
  169. killProc("lyxg");
  170. AppendLog("进程己结束...");
  171. Thread.Sleep(500);
  172. AppendLog("删除目标路径的BIN目录...");
  173. if (Directory.Exists(target + "\\bin")) Directory.Delete(target + "\\bin", true);
  174. AppendLog("覆盖其它所有文件目录...");
  175. DirectoryCopy(folder, target, true);
  176. Thread.Sleep(500);
  177. AppendLog("执行完成!!");
  178. }
  179. catch (Exception ex)
  180. {
  181. AppendLog("发生错误:" + ex.Message);
  182. }
  183. finally
  184. {
  185. btnSure.Enabled = true;
  186. }
  187. }
  188. private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
  189. {
  190. DirectoryInfo dir = new DirectoryInfo(sourceDirName);
  191. DirectoryInfo[] dirs = dir.GetDirectories();
  192. if (!dir.Exists)
  193. {
  194. throw new DirectoryNotFoundException(
  195. "Source directory does not exist or could not be found: "
  196. + sourceDirName);
  197. }
  198. if (!Directory.Exists(destDirName))
  199. {
  200. Directory.CreateDirectory(destDirName);
  201. }
  202. FileInfo[] files = dir.GetFiles();
  203. foreach (FileInfo file in files)
  204. {
  205. string temppath = Path.Combine(destDirName, file.Name);
  206. file.CopyTo(temppath, true);
  207. }
  208. if (copySubDirs)
  209. {
  210. foreach (DirectoryInfo subdir in dirs)
  211. {
  212. string temppath = Path.Combine(destDirName, subdir.Name);
  213. DirectoryCopy(subdir.FullName, temppath, copySubDirs);
  214. }
  215. }
  216. }
  217. public void AppendLog(string msg)
  218. {
  219. AppendLog(msg, Color.Blue);
  220. }
  221. public void AppendLog(string msg, Color c)
  222. {
  223. AppendLog(txtCon, "", msg, c);
  224. }
  225. public void AppendFailLog(string msg, Color c)
  226. {
  227. AppendLog(txtCon, "", msg, c);
  228. }
  229. public void AppendLog(RichTextBox rtb, string tag, string msg, Color c)
  230. {
  231. try
  232. {
  233. string txt = msg + "\r\n";
  234. if (this.IsHandleCreated)
  235. {
  236. this.Invoke(new MethodInvoker(delegate
  237. {
  238. //RemovePrevAppend(rtb);
  239. AppendWithColor(rtb, txt, c);
  240. rtb.SelectionStart = rtb.TextLength;
  241. rtb.ScrollToCaret();
  242. }));
  243. }
  244. else
  245. {
  246. //RemovePrevAppend(rtb);
  247. AppendWithColor(rtb, txt, c);
  248. rtb.SelectionStart = rtb.TextLength;
  249. rtb.ScrollToCaret();
  250. }
  251. }
  252. catch (Exception ex)
  253. {
  254. helper.writeLog("添加RICHTEXTBOX内容时发生错误:" + ex.Message);
  255. }
  256. }
  257. private void RemovePrevAppend(RichTextBox rtb)
  258. {
  259. if (rtb.Lines.Length >= 500)
  260. {
  261. int len = rtb.Lines.Length;
  262. string[] tempArray = rtb.Lines;
  263. string[] sNewLines = new string[100];
  264. Array.Copy(tempArray, len - 101, sNewLines, 0, 100);
  265. rtb.Lines = sNewLines;
  266. }
  267. }
  268. public string getDateTime()
  269. {
  270. return "[" + DateTime.Now.ToString("HH:mm:ss") + "]";
  271. }
  272. public static void AppendWithColor(RichTextBox rtb, string s_c)
  273. {
  274. AppendWithColor(rtb, s_c, Color.Black, null);
  275. }
  276. public static void AppendWithColor(RichTextBox rtb, string s_c, Color c)
  277. {
  278. AppendWithColor(rtb, s_c, c, null);
  279. }
  280. public static void AppendWithColor(RichTextBox rtb, string s_c, Color c, Font f)
  281. {
  282. rtb.AppendText(s_c);
  283. if (rtb.TextLength - s_c.Length < 0) rtb.SelectionStart = 0;
  284. else rtb.SelectionStart = rtb.TextLength - s_c.Length;
  285. rtb.SelectionLength = s_c.Length;
  286. if (c != null) rtb.SelectionColor = c;
  287. if (f != null) rtb.SelectionFont = f;
  288. }
  289. private void stripSetting_Click(object sender, EventArgs e)
  290. {
  291. WindowState = FormWindowState.Normal;
  292. ShowInTaskbar = true;
  293. this.Show();
  294. }
  295. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  296. {
  297. if (e.CloseReason == CloseReason.UserClosing)
  298. {
  299. INI.SetIniValue("SET", "OFolder", txtFolder.Text, iniFile);
  300. INI.SetIniValue("SET", "NFolder", txtActionFolder.Text, iniFile);
  301. // 取消关闭
  302. e.Cancel = true;
  303. this.Hide();
  304. ShowInTaskbar = false;
  305. }
  306. }
  307. private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
  308. {
  309. helper.writeLog("客户端关闭");
  310. }
  311. private void notifyIcon_DoubleClick(object sender, EventArgs e)
  312. {
  313. WindowState = FormWindowState.Normal;
  314. ShowInTaskbar = true;
  315. this.Show();
  316. }
  317. private void stripExit_Click(object sender, EventArgs e)
  318. {
  319. Application.ExitThread();
  320. Application.Exit();
  321. }
  322. private void button1_Click(object sender, EventArgs e)
  323. {
  324. setRunThread();
  325. }
  326. }
  327. }