StartForm.cs 24 KB

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