StartForm.cs 19 KB

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