StartForm.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Threading;
  9. using System.IO;
  10. namespace MOTINOVA_Motor_Factory_Set
  11. {
  12. public partial class StartForm : Form
  13. {
  14. public static ftp myFtp = new ftp();
  15. public static server_cfg myServerCfg = new server_cfg();
  16. public static ImportForm ImportForm1 = new ImportForm();
  17. public static ExportForm ExportForm1 = new ExportForm();
  18. public static ManageForm ManageForm1 = new ManageForm();
  19. public static CheckForm CheckForm1 = new CheckForm();
  20. public static Enter EnterForm1 = new Enter();
  21. public static string UserPath = "";
  22. public StartForm()
  23. {
  24. InitializeComponent();
  25. //配置网络服务参数
  26. myServerCfg.MyServerOption(Directory.GetCurrentDirectory() + "\\Server");
  27. //配置FTP服务器
  28. myFtp.FtpOption(myServerCfg.IP, myServerCfg.Port, myServerCfg.User, myServerCfg.PassWd);
  29. //配置数据存储路径
  30. UserPath = GetLogSavePath(Directory.GetCurrentDirectory() + "\\UserPath");
  31. ImportForm1.LocalCfgFilePath = UserPath + "\\cfg\\";
  32. ImportForm1.LogSavePath = UserPath+ "\\walkTest\\";
  33. CheckForm1.LocalCfgFilePath = UserPath + "\\cfg\\";
  34. CheckForm1.LogSavePath = UserPath + "\\qcTest\\";
  35. ManageForm1.FilePath = UserPath + "\\cfg\\";
  36. ExportForm1.FilePath = UserPath + "\\cfg\\";
  37. //创建线程,定时检测网络连接状态
  38. Thread th = new Thread(NetworkCheck);
  39. th.IsBackground = true;
  40. th.Start();
  41. }
  42. //读取数据存储路径
  43. string GetLogSavePath(string fileName)
  44. {
  45. string result = "";
  46. //加载配置文件
  47. StreamReader objReader = new StreamReader(fileName);
  48. string sLine = "";
  49. List<string> array_CfgInfo = new List<string>();
  50. while (sLine != null)
  51. {
  52. sLine = objReader.ReadLine();
  53. array_CfgInfo.Add(sLine);
  54. }
  55. objReader.Close();
  56. try
  57. {
  58. result = array_CfgInfo[1];
  59. }
  60. catch(Exception e)
  61. {
  62. MessageBox.Show(e.Message);
  63. }
  64. return result;
  65. }
  66. //检查网络状态线程
  67. private void NetworkCheck()
  68. {
  69. //初始化第一次连接FTP服务器
  70. bool Result = myFtp.CheckFtp();
  71. this.Invoke((EventHandler)(delegate
  72. {
  73. if (Result == true)//服务器连接成功
  74. {
  75. label_NetStatus.Text = "网络连接成功";
  76. label_NetStatus.ForeColor = Color.Green;
  77. ImportForm1.label_NetStatus.Text = "网络连接成功";
  78. ImportForm1.label_NetStatus.BackColor = Color.Green;
  79. CheckForm1.label_NetStatus.Text = "网络连接成功";
  80. CheckForm1.label_NetStatus.BackColor = Color.Green;
  81. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  82. {
  83. CheckForm1.checkBox_LocalFile.Checked = false;
  84. CheckForm1.checkBox_LocalFile.Enabled = false;
  85. ImportForm1.checkBox_LocalFile.Checked = false;
  86. ImportForm1.checkBox_LocalFile.Enabled = false;
  87. }
  88. else//允许本地文件,默认在线模式
  89. {
  90. CheckForm1.checkBox_LocalFile.Checked = false;
  91. CheckForm1.checkBox_LocalFile.Enabled = true;
  92. ImportForm1.checkBox_LocalFile.Checked = false;
  93. ImportForm1.checkBox_LocalFile.Enabled = true;
  94. }
  95. myFtp.IsNetConnected = true;
  96. }
  97. else//服务器连接失败
  98. {
  99. label_NetStatus.Text = "网络连接失败";
  100. label_NetStatus.ForeColor = Color.Red;
  101. ImportForm1.label_NetStatus.Text = "网络连接失败";
  102. ImportForm1.label_NetStatus.BackColor = Color.Red;
  103. CheckForm1.label_NetStatus.Text = "网络连接失败";
  104. CheckForm1.label_NetStatus.BackColor = Color.Red;
  105. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  106. {
  107. CheckForm1.checkBox_LocalFile.Checked = false;
  108. CheckForm1.checkBox_LocalFile.Enabled = false;
  109. ImportForm1.checkBox_LocalFile.Checked = false;
  110. ImportForm1.checkBox_LocalFile.Enabled = false;
  111. }
  112. else//允许本地文件
  113. {
  114. CheckForm1.checkBox_LocalFile.Checked = true;
  115. CheckForm1.checkBox_LocalFile.Enabled = false;
  116. ImportForm1.checkBox_LocalFile.Checked = true;
  117. ImportForm1.checkBox_LocalFile.Enabled = false;
  118. }
  119. myFtp.IsNetConnected = false;
  120. if (MessageBox.Show("网络连接失败,是否继续?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
  121. {
  122. this.Close();
  123. }
  124. }
  125. button_Export.Enabled = true;
  126. button_Import.Enabled = true;
  127. button_Mang.Enabled = true;
  128. button_Check.Enabled = true;
  129. }));
  130. //创建定时器,定时3s检查网络
  131. System.Timers.Timer timer_CheckNet = new System.Timers.Timer();
  132. timer_CheckNet.Enabled = true;
  133. timer_CheckNet.Interval = 3000;
  134. timer_CheckNet.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_CheckNet_Tick);
  135. timer_CheckNet.Start();
  136. while (true)
  137. {
  138. Thread.Sleep(1);
  139. }
  140. }
  141. //线程中定时任务,检查网络状态
  142. private void timer_CheckNet_Tick(object sender, EventArgs e)
  143. {
  144. //连接FTP服务器
  145. bool Result = myFtp.CheckFtp();
  146. this.Invoke((EventHandler)(delegate
  147. {
  148. if (Result == true)//服务器连接成功
  149. {
  150. label_NetStatus.Text = "网络连接成功";
  151. label_NetStatus.ForeColor = Color.Green;
  152. ImportForm1.label_NetStatus.Text = "网络连接成功";
  153. ImportForm1.label_NetStatus.BackColor = Color.Green;
  154. CheckForm1.label_NetStatus.Text = "网络连接成功";
  155. CheckForm1.label_NetStatus.BackColor = Color.Green;
  156. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  157. {
  158. CheckForm1.checkBox_LocalFile.Enabled = false;
  159. ImportForm1.checkBox_LocalFile.Enabled = false;
  160. }
  161. else//允许本地文件
  162. {
  163. CheckForm1.checkBox_LocalFile.Enabled = true;
  164. ImportForm1.checkBox_LocalFile.Enabled = true;
  165. }
  166. myFtp.IsNetConnected = true;
  167. }
  168. else//服务器连接失败
  169. {
  170. label_NetStatus.Text = "网络连接失败";
  171. label_NetStatus.ForeColor = Color.Red;
  172. ImportForm1.label_NetStatus.Text = "网络连接失败";
  173. ImportForm1.label_NetStatus.BackColor = Color.Red;
  174. ImportForm1.checkBox_LocalFile.Checked = true;
  175. ImportForm1.checkBox_LocalFile.Enabled = false;
  176. CheckForm1.label_NetStatus.Text = "网络连接失败";
  177. CheckForm1.label_NetStatus.BackColor = Color.Red;
  178. CheckForm1.checkBox_LocalFile.Checked = true;
  179. CheckForm1.checkBox_LocalFile.Enabled = false;
  180. myFtp.IsNetConnected = false;
  181. }
  182. }));
  183. }
  184. private void button_Import_Click(object sender, EventArgs e)
  185. {
  186. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  187. {
  188. if (myFtp.IsNetConnected == false)
  189. {
  190. MessageBox.Show("请检查网络,或联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  191. return;
  192. }
  193. }
  194. CheckForm1.timer1.Enabled = false;
  195. ImportForm1.ShowDialog();
  196. }
  197. private void button_Export_Click(object sender, EventArgs e)
  198. {
  199. EnterForm1.ShowDialog();
  200. if ((EnterForm1.textBox_User.Text == "PT") && (EnterForm1.textBox_Passwd.Text == "123456"))
  201. {
  202. ExportForm1.ShowDialog();
  203. }
  204. else
  205. {
  206. MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  207. }
  208. }
  209. private void button_Mang_Click(object sender, EventArgs e)
  210. {
  211. EnterForm1.ShowDialog();
  212. if ((EnterForm1.textBox_User.Text == "PT") && (EnterForm1.textBox_Passwd.Text == "123456"))
  213. {
  214. ManageForm1.ShowDialog();
  215. }
  216. else
  217. {
  218. MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  219. }
  220. }
  221. private void button_Check_Click(object sender, EventArgs e)
  222. {
  223. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  224. {
  225. if (myFtp.IsNetConnected == false)
  226. {
  227. MessageBox.Show("请检查网络,或联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  228. return;
  229. }
  230. }
  231. ImportForm1.timer1.Enabled = false;
  232. CheckForm1.ShowDialog();
  233. }
  234. private void StartForm_Load(object sender, EventArgs e)
  235. {
  236. //显示编译时间
  237. label_Ver.Text += System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString();
  238. }
  239. private void StartForm_FormClosing(object sender, FormClosingEventArgs e)
  240. {
  241. }
  242. }
  243. }