MainWindow.xaml.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using System.ServiceProcess;
  16. using System.Diagnostics;
  17. namespace PumpDataUI
  18. {
  19. /// <summary>
  20. /// MainWindow.xaml 的交互逻辑
  21. /// </summary>
  22. public partial class MainWindow : Window
  23. {
  24. public MainWindow()
  25. {
  26. InitializeComponent();
  27. }
  28. //安装
  29. private void Button_Click(object sender, RoutedEventArgs e)
  30. {
  31. try
  32. {
  33. string CurrentDirectory = System.Environment.CurrentDirectory;
  34. System.Environment.CurrentDirectory = CurrentDirectory + "\\Service";
  35. Process process = new Process();
  36. process.StartInfo.UseShellExecute = false;
  37. process.StartInfo.FileName = "Install.bat";
  38. process.StartInfo.CreateNoWindow = true;
  39. process.Start();
  40. System.Environment.CurrentDirectory = CurrentDirectory;
  41. MessageBox.Show("安装成功");
  42. }
  43. catch (Exception ex)
  44. {
  45. MessageBox.Show("安装失败:" + ex.ToString());
  46. }
  47. }
  48. //卸载
  49. private void Button_Click_1(object sender, RoutedEventArgs e)
  50. {
  51. try
  52. {
  53. string CurrentDirectory = System.Environment.CurrentDirectory;
  54. System.Environment.CurrentDirectory = CurrentDirectory + "\\Service";
  55. Process process = new Process();
  56. process.StartInfo.UseShellExecute = false;
  57. process.StartInfo.FileName = "Uninstall.bat";
  58. process.StartInfo.CreateNoWindow = true;
  59. process.Start();
  60. System.Environment.CurrentDirectory = CurrentDirectory;
  61. MessageBox.Show("卸载成功");
  62. }
  63. catch (Exception ex)
  64. {
  65. MessageBox.Show("卸载失败:" + ex.ToString());
  66. }
  67. }
  68. //启动
  69. private void Button_Click_2(object sender, RoutedEventArgs e)
  70. {
  71. try
  72. {
  73. ServiceController serviceController = new ServiceController("DinghePumpDataService");
  74. if (serviceController.Status != ServiceControllerStatus.Running)
  75. serviceController.Start();
  76. MessageBox.Show("启动成功");
  77. }
  78. catch (Exception ex)
  79. {
  80. MessageBox.Show("启动失败:" + ex.ToString());
  81. }
  82. }
  83. //停止
  84. private void Button_Click_3(object sender, RoutedEventArgs e)
  85. {
  86. try
  87. {
  88. ServiceController serviceController = new ServiceController("DinghePumpDataService");
  89. if (serviceController.CanStop)
  90. serviceController.Stop();
  91. MessageBox.Show("停止成功");
  92. }
  93. catch (Exception ex)
  94. {
  95. MessageBox.Show("停止失败:" + ex.ToString());
  96. }
  97. }
  98. //暂停/继续
  99. private void Button_Click_4(object sender, RoutedEventArgs e)
  100. {
  101. try
  102. {
  103. ServiceController serviceController = new ServiceController("DinghePumpDataService");
  104. if (serviceController.CanPauseAndContinue)
  105. {
  106. if (serviceController.Status == ServiceControllerStatus.Running)
  107. serviceController.Pause();
  108. else if (serviceController.Status == ServiceControllerStatus.Paused)
  109. serviceController.Continue();
  110. MessageBox.Show("暂停/继续成功");
  111. }
  112. else
  113. {
  114. MessageBox.Show("服务不支持暂停/继续");
  115. }
  116. }
  117. catch (Exception ex)
  118. {
  119. MessageBox.Show("暂停/继续失败:" + ex.ToString());
  120. }
  121. }
  122. //检查状态
  123. private void Button_Click_5(object sender, RoutedEventArgs e)
  124. {
  125. //TimeSpan ts1 = new TimeSpan(DateTime.Now.Ticks);
  126. //TimeSpan ts2 = new TimeSpan(DateTime.Today.Ticks);
  127. //TimeSpan ts = ts1.Subtract(ts2).Duration();
  128. //bool istoday = ts.Days == 0;
  129. //string s1 = "3:15";
  130. //string s2 = "1:40";
  131. ////int a = s1.IndexOf(s2);
  132. //int a = string.Compare(s1, s2);
  133. ServiceController serviceController = new ServiceController("DinghePumpDataService");
  134. string status = serviceController.Status.ToString();
  135. MessageBox.Show(status);
  136. //string sql = string.Format("select * from vdncallrecord where s_date>='{0}' and s_date<'{1}'", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Today.AddDays(1).ToString("yyyy-MM-dd"));
  137. //DateTime dt = GetWeekFirstDayMon(DateTime.Now);
  138. //string kk = "1234";
  139. //string bb = kk.Substring(0, kk.Length - 1);
  140. }
  141. #region 测试
  142. /// <summary>
  143. /// 得到本周第一天(以星期一为第一天)
  144. /// </summary>
  145. /// <param name="datetime"></param>
  146. /// <returns></returns>
  147. private DateTime GetWeekFirstDayMon(DateTime datetime)
  148. {
  149. //星期一为第一天
  150. int weeknow = Convert.ToInt32(datetime.DayOfWeek);
  151. //因为是以星期一为第一天,所以要判断weeknow等于0时,要向前推6天。
  152. weeknow = (weeknow == 0 ? (7 - 1) : (weeknow - 1));
  153. int daydiff = (-1) * weeknow;
  154. //本周第一天
  155. string FirstDay = datetime.AddDays(daydiff).ToString("yyyy-MM-dd");
  156. return Convert.ToDateTime(FirstDay);
  157. }
  158. #endregion
  159. }
  160. }