StartForm.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Windows.Forms;
  5. using System.Threading;
  6. using System.IO;
  7. using System.Linq;
  8. namespace MOTINOVA_Motor_Factory_Set
  9. {
  10. public partial class StartForm : Form
  11. {
  12. public static ftp myFtp = new ftp();
  13. public static server_cfg myServerCfg = new server_cfg();
  14. public static ImportForm ImportForm1 = new ImportForm();
  15. public static ExportForm ExportForm1 = new ExportForm();
  16. public static ManageForm ManageForm1 = new ManageForm();
  17. public static CheckForm CheckForm1 = new CheckForm();
  18. public static Enter EnterForm1 = new Enter();
  19. public static string UserPath = "";
  20. public StartForm()
  21. {
  22. InitializeComponent();
  23. //配置网络服务参数
  24. myServerCfg.MyServerOption(Directory.GetCurrentDirectory() + "\\Server");
  25. //配置FTP服务器
  26. myFtp.FtpOption(myServerCfg.IP, myServerCfg.Port, myServerCfg.User, myServerCfg.PassWd);
  27. //配置数据存储路径
  28. UserPath = GetLogSavePath(Directory.GetCurrentDirectory() + "\\UserPath");
  29. ImportForm1.LocalCfgFilePath = UserPath + "\\cfg\\";
  30. ImportForm1.LogSavePath = UserPath+ "\\walkTest\\";
  31. CheckForm1.LocalCfgFilePath = UserPath + "\\cfg\\";
  32. CheckForm1.LogSavePath = UserPath + "\\qcTest\\";
  33. ManageForm1.FilePath = UserPath + "\\cfg\\";
  34. ExportForm1.FilePath = UserPath + "\\cfg\\";
  35. //创建线程,定时检测网络连接状态
  36. Thread th = new Thread(NetworkCheck);
  37. th.IsBackground = true;
  38. th.Start();
  39. }
  40. //读取数据存储路径
  41. string GetLogSavePath(string fileName)
  42. {
  43. string result = "";
  44. //加载配置文件
  45. StreamReader objReader = new StreamReader(fileName);
  46. string sLine = "";
  47. List<string> array_CfgInfo = new List<string>();
  48. while (sLine != null)
  49. {
  50. sLine = objReader.ReadLine();
  51. array_CfgInfo.Add(sLine);
  52. }
  53. objReader.Close();
  54. try
  55. {
  56. result = array_CfgInfo[1];
  57. }
  58. catch(Exception e)
  59. {
  60. MessageBox.Show(e.Message);
  61. }
  62. return result;
  63. }
  64. //检查网络状态线程
  65. private void NetworkCheck()
  66. {
  67. //初始化第一次连接FTP服务器
  68. bool Result = myFtp.CheckFtp();
  69. this.Invoke((EventHandler)(delegate
  70. {
  71. if (Result == true)//服务器连接成功
  72. {
  73. label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  74. label_NetStatus.ForeColor = Color.Green;
  75. ImportForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  76. ImportForm1.label_NetStatus.BackColor = Color.Green;
  77. CheckForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  78. CheckForm1.label_NetStatus.BackColor = Color.Green;
  79. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  80. {
  81. CheckForm1.checkBox_LocalFile.Checked = false;
  82. CheckForm1.checkBox_LocalFile.Enabled = false;
  83. ImportForm1.checkBox_LocalFile.Checked = false;
  84. ImportForm1.checkBox_LocalFile.Enabled = false;
  85. }
  86. else//允许本地文件,默认在线模式
  87. {
  88. CheckForm1.checkBox_LocalFile.Checked = false;
  89. CheckForm1.checkBox_LocalFile.Enabled = true;
  90. ImportForm1.checkBox_LocalFile.Checked = false;
  91. ImportForm1.checkBox_LocalFile.Enabled = true;
  92. }
  93. myFtp.IsNetConnected = true;
  94. }
  95. else//服务器连接失败
  96. {
  97. label_NetStatus.Text = "网络连接失败";
  98. label_NetStatus.ForeColor = Color.Red;
  99. ImportForm1.label_NetStatus.Text = "网络连接失败";
  100. ImportForm1.label_NetStatus.BackColor = Color.Red;
  101. CheckForm1.label_NetStatus.Text = "网络连接失败";
  102. CheckForm1.label_NetStatus.BackColor = Color.Red;
  103. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  104. {
  105. CheckForm1.checkBox_LocalFile.Checked = false;
  106. CheckForm1.checkBox_LocalFile.Enabled = false;
  107. ImportForm1.checkBox_LocalFile.Checked = false;
  108. ImportForm1.checkBox_LocalFile.Enabled = false;
  109. }
  110. else//允许本地文件
  111. {
  112. CheckForm1.checkBox_LocalFile.Checked = true;
  113. CheckForm1.checkBox_LocalFile.Enabled = false;
  114. ImportForm1.checkBox_LocalFile.Checked = true;
  115. ImportForm1.checkBox_LocalFile.Enabled = false;
  116. }
  117. myFtp.IsNetConnected = false;
  118. if (MessageBox.Show("网络连接失败,是否继续?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
  119. {
  120. this.Close();
  121. }
  122. }
  123. button_Export.Enabled = true;
  124. button_Import.Enabled = true;
  125. button_Mang.Enabled = true;
  126. button_Check.Enabled = true;
  127. }));
  128. //创建定时器,定时10s检查网络
  129. System.Timers.Timer timer_CheckNet = new System.Timers.Timer();
  130. timer_CheckNet.Enabled = true;
  131. timer_CheckNet.Interval = 10000;
  132. timer_CheckNet.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_CheckNet_Tick);
  133. timer_CheckNet.Start();
  134. while (true)
  135. {
  136. Thread.Sleep(1);
  137. }
  138. }
  139. //线程中定时任务,检查网络状态
  140. private void timer_CheckNet_Tick(object sender, EventArgs e)
  141. {
  142. //连接FTP服务器
  143. bool Result = myFtp.CheckFtp();
  144. this.Invoke((EventHandler)(delegate
  145. {
  146. if (Result == true)//服务器连接成功
  147. {
  148. label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  149. label_NetStatus.ForeColor = Color.Green;
  150. ImportForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  151. ImportForm1.label_NetStatus.BackColor = Color.Green;
  152. CheckForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  153. CheckForm1.label_NetStatus.BackColor = Color.Green;
  154. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  155. {
  156. CheckForm1.checkBox_LocalFile.Enabled = false;
  157. ImportForm1.checkBox_LocalFile.Enabled = false;
  158. }
  159. else//允许本地文件
  160. {
  161. CheckForm1.checkBox_LocalFile.Enabled = true;
  162. ImportForm1.checkBox_LocalFile.Enabled = true;
  163. }
  164. myFtp.IsNetConnected = true;
  165. //定时与服务器同步文件
  166. #if true
  167. //cfg
  168. PathSyncWithServer("/cfg");
  169. //qcTest/errorlog
  170. PathSyncWithServer("/qcTest/errorlog");
  171. //qcTest/log
  172. PathSyncWithServer("/qcTest/log");
  173. //walkTest/errorlog
  174. PathSyncWithServer("/walkTest/errorlog");
  175. //walkTest/log
  176. PathSyncWithServer("/walkTest/log");
  177. #endif
  178. }
  179. else//服务器连接失败
  180. {
  181. label_NetStatus.Text = "网络连接失败";
  182. label_NetStatus.ForeColor = Color.Red;
  183. ImportForm1.label_NetStatus.Text = "网络连接失败";
  184. ImportForm1.label_NetStatus.BackColor = Color.Red;
  185. ImportForm1.checkBox_LocalFile.Checked = true;
  186. ImportForm1.checkBox_LocalFile.Enabled = false;
  187. CheckForm1.label_NetStatus.Text = "网络连接失败";
  188. CheckForm1.label_NetStatus.BackColor = Color.Red;
  189. CheckForm1.checkBox_LocalFile.Checked = true;
  190. CheckForm1.checkBox_LocalFile.Enabled = false;
  191. myFtp.IsNetConnected = false;
  192. }
  193. }));
  194. }
  195. /// <summary>
  196. /// 获取本地和远端目录名差集,本地有但远端没有
  197. /// </summary>
  198. /// <param name="localPath">本地路径</param>
  199. /// <param name="remotePath">远端路径</param>
  200. /// <returns>差集文件夹名称集合</returns>
  201. private string[] FiltDiffDirectory(string localPath, string remotePath)
  202. {
  203. try
  204. {
  205. string[] Local = System.IO.Directory.GetDirectories(localPath);
  206. for (int i = 0; i < Local.Length; i++)
  207. Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
  208. string[] Remote = StartForm.myFtp.GetDirectoryList(remotePath);
  209. for (int i = 0; i < Remote.Length; i++)
  210. Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
  211. return Local.Except(Remote).ToList().ToArray();
  212. }
  213. catch (Exception ex)
  214. {
  215. throw new Exception("同步失败,原因: " + ex.Message);
  216. }
  217. }
  218. /// <summary>
  219. /// 获取本地和远端文件夹下目录名交集
  220. /// </summary>
  221. /// <param name="localPath"></param>
  222. /// <param name="remotePath"></param>
  223. /// <returns></returns>
  224. private string[] FiltCommonDirectory(string localPath, string remotePath)
  225. {
  226. try
  227. {
  228. string[] Local = System.IO.Directory.GetDirectories(localPath);
  229. for (int i = 0; i < Local.Length; i++)
  230. Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
  231. string[] Remote = StartForm.myFtp.GetDirectoryList(remotePath);
  232. for (int i = 0; i < Remote.Length; i++)
  233. Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
  234. return Local.Intersect(Remote).ToList().ToArray();
  235. }
  236. catch (Exception ex)
  237. {
  238. throw new Exception("同步失败,原因: " + ex.Message);
  239. }
  240. }
  241. /// <summary>
  242. /// 获取本地和远端文件名差集,本地存在而远端不存在
  243. /// </summary>
  244. /// <param name="localPath">本地路径</param>
  245. /// <param name="remotePath">远端路径</param>
  246. /// <returns>差集文件名集合</returns>
  247. private string[] FiltDiffFiles(string localPath, string remotePath)
  248. {
  249. try
  250. {
  251. string[] Local = System.IO.Directory.GetFiles(localPath);
  252. for (int i = 0; i < Local.Length; i++)
  253. Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
  254. string[] Remote = StartForm.myFtp.GetFileNameList(remotePath);
  255. for (int i = 0; i < Remote.Length; i++)
  256. Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
  257. return Local.Except(Remote).ToList().ToArray();
  258. }
  259. catch (Exception ex)
  260. {
  261. throw new Exception("同步失败,原因: " + ex.Message);
  262. }
  263. }
  264. /// <summary>
  265. /// 获取本地和远端文件名交集
  266. /// </summary>
  267. /// <param name="localPath"></param>
  268. /// <param name="remotePath"></param>
  269. /// <returns></returns>
  270. private string[] FiltCommonFiles(string localPath, string remotePath)
  271. {
  272. try
  273. {
  274. string[] Local = System.IO.Directory.GetFiles(localPath);
  275. for (int i = 0; i < Local.Length; i++)
  276. Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
  277. string[] Remote = StartForm.myFtp.GetFileNameList(remotePath);
  278. for (int i = 0; i < Remote.Length; i++)
  279. Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
  280. return Local.Intersect(Remote).ToList().ToArray();
  281. }
  282. catch (Exception ex)
  283. {
  284. throw new Exception("同步失败,原因: " + ex.Message);
  285. }
  286. }
  287. /// <summary>
  288. /// 同步本地和远端文件夹
  289. /// 本地有、远端无的文件夹,上传该目录下本地所有文件
  290. /// 本地有和远端有的文件夹,上传本地有但远端没有的文件
  291. /// </summary>
  292. /// <param name="PathName"></param>
  293. /// <returns></returns>
  294. private void PathSyncWithServer(string PathName)
  295. {
  296. List<string> pathResult = null;
  297. //获取本地和远端的目录差集
  298. pathResult = FiltDiffDirectory(UserPath + PathName, myServerCfg.RootPath + PathName).ToList();
  299. if (pathResult.Count != 0)
  300. {
  301. foreach (string path in pathResult)
  302. {
  303. //远端建立文件夹
  304. myFtp.MakeDir(myServerCfg.RootPath + PathName + "/" + path);
  305. //获取待上传文件列表
  306. string[] fileList = Directory.GetFiles(UserPath + PathName + "\\" + path);
  307. for (int i = 0; i < fileList.Length; i++)
  308. fileList[i] = fileList[i].Substring(fileList[i].LastIndexOf("\\") + 1);
  309. //上传文件
  310. if (fileList.Length != 0)
  311. {
  312. foreach (string file in fileList)
  313. {
  314. myFtp.UploadFile(UserPath + PathName + "\\" + path + "\\" + file, myServerCfg.RootPath + PathName + "/" + path);
  315. }
  316. }
  317. }
  318. }
  319. //获取本地和远端的目录交集
  320. pathResult = FiltCommonDirectory(UserPath + PathName, myServerCfg.RootPath + PathName).ToList();
  321. if (pathResult.Count != 0)
  322. {
  323. foreach (string path in pathResult)
  324. {
  325. //获取该目录本地和远端文件名的差集,本地有但远端无
  326. string[] fileList = FiltDiffFiles(UserPath + PathName + "\\" + path, myServerCfg.RootPath + PathName + "/" + path);
  327. //上传文件
  328. if (fileList.Length != 0)
  329. {
  330. foreach (string file in fileList)
  331. {
  332. myFtp.UploadFile(UserPath + PathName + "\\" + path + "\\" + file, myServerCfg.RootPath + PathName + "/" + path);
  333. }
  334. }
  335. }
  336. }
  337. }
  338. private void button_Import_Click(object sender, EventArgs e)
  339. {
  340. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  341. {
  342. if (myFtp.IsNetConnected == false)
  343. {
  344. MessageBox.Show("请检查网络,或联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  345. return;
  346. }
  347. }
  348. CheckForm1.timer1.Enabled = false;
  349. ImportForm1.ShowDialog();
  350. }
  351. private void button_Export_Click(object sender, EventArgs e)
  352. {
  353. EnterForm1.ShowDialog();
  354. if ((EnterForm1.textBox_User.Text == "PT") && (EnterForm1.textBox_Passwd.Text == "123456"))
  355. {
  356. ExportForm1.ShowDialog();
  357. }
  358. else
  359. {
  360. MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  361. }
  362. }
  363. private void button_Mang_Click(object sender, EventArgs e)
  364. {
  365. EnterForm1.ShowDialog();
  366. if ((EnterForm1.textBox_User.Text == "PT") && (EnterForm1.textBox_Passwd.Text == "123456"))
  367. {
  368. ManageForm1.ShowDialog();
  369. }
  370. else
  371. {
  372. MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  373. }
  374. }
  375. private void button_Check_Click(object sender, EventArgs e)
  376. {
  377. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  378. {
  379. if (myFtp.IsNetConnected == false)
  380. {
  381. MessageBox.Show("请检查网络,或联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  382. return;
  383. }
  384. }
  385. ImportForm1.timer1.Enabled = false;
  386. CheckForm1.ShowDialog();
  387. }
  388. private void StartForm_Load(object sender, EventArgs e)
  389. {
  390. //显示编译时间
  391. label_Ver.Text += System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString();
  392. }
  393. private void StartForm_FormClosing(object sender, FormClosingEventArgs e)
  394. {
  395. }
  396. }
  397. }