StartForm.cs 19 KB

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