StartForm.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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. ManageForm1.textBox_LocalPath.Text = UserPath;
  36. //检查本地文件夹
  37. if (!Directory.Exists(ImportForm1.LocalCfgFilePath))
  38. Directory.CreateDirectory(ImportForm1.LocalCfgFilePath);
  39. if (!Directory.Exists(ImportForm1.LogSavePath))
  40. Directory.CreateDirectory(ImportForm1.LogSavePath);
  41. if (!Directory.Exists(CheckForm1.LocalCfgFilePath))
  42. Directory.CreateDirectory(CheckForm1.LocalCfgFilePath);
  43. if (!Directory.Exists(CheckForm1.LogSavePath))
  44. Directory.CreateDirectory(CheckForm1.LogSavePath);
  45. if (!Directory.Exists(ManageForm1.FilePath))
  46. Directory.CreateDirectory(ManageForm1.FilePath);
  47. if (!Directory.Exists(ExportForm1.FilePath))
  48. Directory.CreateDirectory(ExportForm1.FilePath);
  49. //创建线程,定时检测网络连接状态
  50. Thread th = new Thread(NetworkCheck);
  51. th.IsBackground = true;
  52. th.Start();
  53. }
  54. //读取数据存储路径
  55. string GetLogSavePath(string fileName)
  56. {
  57. string result = "";
  58. try
  59. {
  60. //加载配置文件
  61. StreamReader objReader = new StreamReader(fileName);
  62. string sLine = "";
  63. List<string> array_CfgInfo = new List<string>();
  64. while (sLine != null)
  65. {
  66. sLine = objReader.ReadLine();
  67. array_CfgInfo.Add(sLine);
  68. }
  69. objReader.Close();
  70. result = array_CfgInfo[0].Substring(array_CfgInfo[0].LastIndexOf("=") + 1);
  71. if (result == "")
  72. {
  73. MessageBox.Show("文件加载失败,采用默认路径");
  74. result = Directory.GetCurrentDirectory();
  75. }
  76. }
  77. catch(Exception e)
  78. {
  79. MessageBox.Show("文件加载失败,采用默认路径",e.Message);
  80. result = Directory.GetCurrentDirectory();
  81. }
  82. return result;
  83. }
  84. //检查网络状态线程
  85. private void NetworkCheck()
  86. {
  87. //初始化第一次连接FTP服务器
  88. bool Result = myFtp.CheckFtp();
  89. this.Invoke((EventHandler)(delegate
  90. {
  91. if (Result == true)//服务器连接成功
  92. {
  93. label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  94. label_NetStatus.ForeColor = Color.Green;
  95. ImportForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  96. ImportForm1.label_NetStatus.BackColor = Color.Green;
  97. CheckForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  98. CheckForm1.label_NetStatus.BackColor = Color.Green;
  99. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  100. {
  101. CheckForm1.checkBox_LocalFile.Checked = false;
  102. CheckForm1.checkBox_LocalFile.Enabled = false;
  103. ImportForm1.checkBox_LocalFile.Checked = false;
  104. ImportForm1.checkBox_LocalFile.Enabled = false;
  105. }
  106. else//允许本地文件,默认在线模式
  107. {
  108. CheckForm1.checkBox_LocalFile.Checked = false;
  109. CheckForm1.checkBox_LocalFile.Enabled = true;
  110. ImportForm1.checkBox_LocalFile.Checked = false;
  111. ImportForm1.checkBox_LocalFile.Enabled = true;
  112. }
  113. myFtp.IsNetConnected = true;
  114. }
  115. else//服务器连接失败
  116. {
  117. label_NetStatus.Text = "网络连接失败";
  118. label_NetStatus.ForeColor = Color.Red;
  119. ImportForm1.label_NetStatus.Text = "网络连接失败";
  120. ImportForm1.label_NetStatus.BackColor = Color.Red;
  121. CheckForm1.label_NetStatus.Text = "网络连接失败";
  122. CheckForm1.label_NetStatus.BackColor = Color.Red;
  123. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  124. {
  125. CheckForm1.checkBox_LocalFile.Checked = false;
  126. CheckForm1.checkBox_LocalFile.Enabled = false;
  127. ImportForm1.checkBox_LocalFile.Checked = false;
  128. ImportForm1.checkBox_LocalFile.Enabled = false;
  129. }
  130. else//允许本地文件
  131. {
  132. CheckForm1.checkBox_LocalFile.Checked = true;
  133. CheckForm1.checkBox_LocalFile.Enabled = false;
  134. ImportForm1.checkBox_LocalFile.Checked = true;
  135. ImportForm1.checkBox_LocalFile.Enabled = false;
  136. }
  137. myFtp.IsNetConnected = false;
  138. if (MessageBox.Show("网络连接失败,是否继续?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
  139. {
  140. this.Close();
  141. }
  142. }
  143. button_Export.Enabled = true;
  144. button_Import.Enabled = true;
  145. button_Mang.Enabled = true;
  146. button_Check.Enabled = true;
  147. }));
  148. //创建定时器,定时10s检查网络
  149. System.Timers.Timer timer_CheckNet = new System.Timers.Timer();
  150. timer_CheckNet.Enabled = true;
  151. timer_CheckNet.Interval = 10000;
  152. timer_CheckNet.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_CheckNet_Tick);
  153. timer_CheckNet.Start();
  154. while (true)
  155. {
  156. Thread.Sleep(1);
  157. }
  158. }
  159. //线程中定时任务,检查网络状态
  160. private void timer_CheckNet_Tick(object sender, EventArgs e)
  161. {
  162. //连接FTP服务器
  163. bool Result = myFtp.CheckFtp();
  164. this.Invoke((EventHandler)(delegate
  165. {
  166. if (Result == true)//服务器连接成功
  167. {
  168. label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  169. label_NetStatus.ForeColor = Color.Green;
  170. ImportForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  171. ImportForm1.label_NetStatus.BackColor = Color.Green;
  172. CheckForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
  173. CheckForm1.label_NetStatus.BackColor = Color.Green;
  174. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  175. {
  176. CheckForm1.checkBox_LocalFile.Enabled = false;
  177. ImportForm1.checkBox_LocalFile.Enabled = false;
  178. }
  179. else//允许本地文件
  180. {
  181. CheckForm1.checkBox_LocalFile.Enabled = true;
  182. ImportForm1.checkBox_LocalFile.Enabled = true;
  183. }
  184. myFtp.IsNetConnected = true;
  185. //定时与服务器同步文件
  186. #if false
  187. //cfg
  188. PathSyncWithServer("/cfg");
  189. //qcTest/errorlog
  190. PathSyncWithServer("/qcTest/errorlog");
  191. //qcTest/log
  192. PathSyncWithServer("/qcTest/log");
  193. //walkTest/errorlog
  194. PathSyncWithServer("/walkTest/errorlog");
  195. //walkTest/log
  196. PathSyncWithServer("/walkTest/log");
  197. #endif
  198. }
  199. else//服务器连接失败
  200. {
  201. label_NetStatus.Text = "网络连接失败";
  202. label_NetStatus.ForeColor = Color.Red;
  203. ImportForm1.label_NetStatus.Text = "网络连接失败";
  204. ImportForm1.label_NetStatus.BackColor = Color.Red;
  205. ImportForm1.checkBox_LocalFile.Checked = true;
  206. ImportForm1.checkBox_LocalFile.Enabled = false;
  207. CheckForm1.label_NetStatus.Text = "网络连接失败";
  208. CheckForm1.label_NetStatus.BackColor = Color.Red;
  209. CheckForm1.checkBox_LocalFile.Checked = true;
  210. CheckForm1.checkBox_LocalFile.Enabled = false;
  211. myFtp.IsNetConnected = false;
  212. }
  213. }));
  214. }
  215. /// <summary>
  216. /// 获取本地和远端目录名差集,本地有但远端没有
  217. /// </summary>
  218. /// <param name="localPath">本地路径</param>
  219. /// <param name="remotePath">远端路径</param>
  220. /// <returns>差集文件夹名称集合</returns>
  221. private string[] FiltDiffDirectory(string localPath, string remotePath)
  222. {
  223. try
  224. {
  225. string[] Local = System.IO.Directory.GetDirectories(localPath);
  226. for (int i = 0; i < Local.Length; i++)
  227. Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
  228. string[] Remote = StartForm.myFtp.GetDirectoryList(remotePath);
  229. for (int i = 0; i < Remote.Length; i++)
  230. Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
  231. return Local.Except(Remote).ToList().ToArray();
  232. }
  233. catch (Exception ex)
  234. {
  235. throw new Exception("同步失败,原因: " + ex.Message);
  236. }
  237. }
  238. /// <summary>
  239. /// 获取本地和远端文件夹下目录名交集
  240. /// </summary>
  241. /// <param name="localPath"></param>
  242. /// <param name="remotePath"></param>
  243. /// <returns></returns>
  244. private string[] FiltCommonDirectory(string localPath, string remotePath)
  245. {
  246. try
  247. {
  248. string[] Local = System.IO.Directory.GetDirectories(localPath);
  249. for (int i = 0; i < Local.Length; i++)
  250. Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
  251. string[] Remote = StartForm.myFtp.GetDirectoryList(remotePath);
  252. for (int i = 0; i < Remote.Length; i++)
  253. Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
  254. return Local.Intersect(Remote).ToList().ToArray();
  255. }
  256. catch (Exception ex)
  257. {
  258. throw new Exception("同步失败,原因: " + ex.Message);
  259. }
  260. }
  261. /// <summary>
  262. /// 获取本地和远端文件名差集,本地存在而远端不存在
  263. /// </summary>
  264. /// <param name="localPath">本地路径</param>
  265. /// <param name="remotePath">远端路径</param>
  266. /// <returns>差集文件名集合</returns>
  267. private string[] FiltDiffFiles(string localPath, string remotePath)
  268. {
  269. try
  270. {
  271. string[] Local = System.IO.Directory.GetFiles(localPath);
  272. for (int i = 0; i < Local.Length; i++)
  273. Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
  274. string[] Remote = StartForm.myFtp.GetFileNameList(remotePath);
  275. for (int i = 0; i < Remote.Length; i++)
  276. Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
  277. return Local.Except(Remote).ToList().ToArray();
  278. }
  279. catch (Exception ex)
  280. {
  281. throw new Exception("同步失败,原因: " + ex.Message);
  282. }
  283. }
  284. /// <summary>
  285. /// 获取本地和远端文件名交集
  286. /// </summary>
  287. /// <param name="localPath"></param>
  288. /// <param name="remotePath"></param>
  289. /// <returns></returns>
  290. private string[] FiltCommonFiles(string localPath, string remotePath)
  291. {
  292. try
  293. {
  294. string[] Local = System.IO.Directory.GetFiles(localPath);
  295. for (int i = 0; i < Local.Length; i++)
  296. Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
  297. string[] Remote = StartForm.myFtp.GetFileNameList(remotePath);
  298. for (int i = 0; i < Remote.Length; i++)
  299. Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
  300. return Local.Intersect(Remote).ToList().ToArray();
  301. }
  302. catch (Exception ex)
  303. {
  304. throw new Exception("同步失败,原因: " + ex.Message);
  305. }
  306. }
  307. /// <summary>
  308. /// 同步本地和远端文件夹
  309. /// 本地有、远端无的文件夹,上传该目录下本地所有文件
  310. /// 本地有和远端有的文件夹,上传本地有但远端没有的文件
  311. /// </summary>
  312. /// <param name="PathName"></param>
  313. /// <returns></returns>
  314. private void PathSyncWithServer(string PathName)
  315. {
  316. List<string> pathResult = null;
  317. //获取本地和远端的目录差集
  318. pathResult = FiltDiffDirectory(UserPath + PathName, myServerCfg.RootPath + PathName).ToList();
  319. if (pathResult.Count != 0)
  320. {
  321. foreach (string path in pathResult)
  322. {
  323. //远端建立文件夹
  324. myFtp.MakeDir(myServerCfg.RootPath + PathName + "/" + path);
  325. //获取待上传文件列表
  326. string[] fileList = Directory.GetFiles(UserPath + PathName + "\\" + path);
  327. for (int i = 0; i < fileList.Length; i++)
  328. fileList[i] = fileList[i].Substring(fileList[i].LastIndexOf("\\") + 1);
  329. //上传文件
  330. if (fileList.Length != 0)
  331. {
  332. foreach (string file in fileList)
  333. {
  334. myFtp.UploadFile(UserPath + PathName + "\\" + path + "\\" + file, myServerCfg.RootPath + PathName + "/" + path);
  335. }
  336. }
  337. }
  338. }
  339. //获取本地和远端的目录交集
  340. pathResult = FiltCommonDirectory(UserPath + PathName, myServerCfg.RootPath + PathName).ToList();
  341. if (pathResult.Count != 0)
  342. {
  343. foreach (string path in pathResult)
  344. {
  345. //获取该目录本地和远端文件名的差集,本地有但远端无
  346. string[] fileList = FiltDiffFiles(UserPath + PathName + "\\" + path, myServerCfg.RootPath + PathName + "/" + path);
  347. //上传文件
  348. if (fileList.Length != 0)
  349. {
  350. foreach (string file in fileList)
  351. {
  352. myFtp.UploadFile(UserPath + PathName + "\\" + path + "\\" + file, myServerCfg.RootPath + PathName + "/" + path);
  353. }
  354. }
  355. }
  356. }
  357. }
  358. private void button_Import_Click(object sender, EventArgs e)
  359. {
  360. //不允许离线使用时检查网络
  361. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  362. {
  363. if (myFtp.IsNetConnected == false)
  364. {
  365. MessageBox.Show("请检查网络,或联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  366. return;
  367. }
  368. }
  369. //输入用户名
  370. EnterForm1.ShowDialog();
  371. try
  372. {
  373. if (EnterForm1.textBox_Passwd.Text == EnterForm1.UserAccount[EnterForm1.comboBox_User.Text])//检验模式所有用户支持
  374. {
  375. CheckForm1.timer1.Enabled = false;
  376. ImportForm1.ShowDialog();
  377. }
  378. else
  379. {
  380. MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  381. }
  382. }
  383. catch (Exception)
  384. {
  385. MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  386. }
  387. }
  388. private void button_Export_Click(object sender, EventArgs e)
  389. {
  390. EnterForm1.ShowDialog();
  391. try
  392. {
  393. if(EnterForm1.textBox_Passwd.Text == EnterForm1.UserAccount[EnterForm1.comboBox_User.Text])//生成参数模式仅用户PT
  394. {
  395. if (EnterForm1.comboBox_User.Text == "工程Engineer")
  396. {
  397. ExportForm1.ShowDialog();
  398. }
  399. else
  400. {
  401. MessageBox.Show("权限不支持", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  402. }
  403. }
  404. else
  405. {
  406. MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  407. }
  408. }
  409. catch (Exception)
  410. {
  411. MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  412. }
  413. }
  414. private void button_Mang_Click(object sender, EventArgs e)
  415. {
  416. EnterForm1.ShowDialog();
  417. try
  418. {
  419. if (EnterForm1.textBox_Passwd.Text == EnterForm1.UserAccount[EnterForm1.comboBox_User.Text])//本地文件管理仅支持PT用户
  420. {
  421. if (EnterForm1.comboBox_User.Text == "工程Engineer")
  422. {
  423. ManageForm1.ShowDialog();
  424. }
  425. else
  426. {
  427. MessageBox.Show("权限不支持", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  428. }
  429. }
  430. else
  431. {
  432. MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  433. }
  434. }
  435. catch (Exception)
  436. {
  437. MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  438. }
  439. }
  440. private void button_Check_Click(object sender, EventArgs e)
  441. {
  442. //不允许离线使用时,检查网络
  443. if (myServerCfg.Local == "DISABLE")//不允许本地文件
  444. {
  445. if (myFtp.IsNetConnected == false)
  446. {
  447. MessageBox.Show("请检查网络,或联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
  448. return;
  449. }
  450. }
  451. //输入用户名
  452. EnterForm1.ShowDialog();
  453. try
  454. {
  455. if (EnterForm1.textBox_Passwd.Text == EnterForm1.UserAccount[EnterForm1.comboBox_User.Text])//所有用户支持
  456. {
  457. ImportForm1.timer1.Enabled = false;
  458. CheckForm1.ShowDialog();
  459. }
  460. else
  461. {
  462. MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  463. }
  464. }
  465. catch (Exception)
  466. {
  467. MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  468. }
  469. }
  470. private void StartForm_Load(object sender, EventArgs e)
  471. {
  472. }
  473. private void StartForm_FormClosing(object sender, FormClosingEventArgs e)
  474. {
  475. //关闭前,删除本地所有配置文件
  476. string path = StartForm.UserPath + "\\cfg\\"; ;
  477. try
  478. {
  479. DirectoryInfo dir = new DirectoryInfo(path);
  480. FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录
  481. foreach (FileSystemInfo i in fileinfo)
  482. {
  483. if (i is DirectoryInfo) //判断是否文件夹
  484. {
  485. DirectoryInfo subdir = new DirectoryInfo(i.FullName);
  486. subdir.Delete(true); //删除子目录和文件
  487. }
  488. else
  489. {
  490. File.Delete(i.FullName); //删除指定文件
  491. }
  492. }
  493. //MessageBox.Show("文件清理成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  494. }
  495. catch (Exception)
  496. {
  497. MessageBox.Show("文件清理失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  498. }
  499. }
  500. }
  501. }