123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438 |
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Threading;
- using System.IO;
- using System.Linq;
- namespace MOTINOVA_Motor_Factory_Set
- {
- public partial class StartForm : Form
- {
- public static ftp myFtp = new ftp();
- public static server_cfg myServerCfg = new server_cfg();
- public static ImportForm ImportForm1 = new ImportForm();
- public static ExportForm ExportForm1 = new ExportForm();
- public static ManageForm ManageForm1 = new ManageForm();
- public static CheckForm CheckForm1 = new CheckForm();
- public static Enter EnterForm1 = new Enter();
- public static string UserPath = "";
- public StartForm()
- {
- InitializeComponent();
- //配置网络服务参数
- myServerCfg.MyServerOption(Directory.GetCurrentDirectory() + "\\Server");
- //配置FTP服务器
- myFtp.FtpOption(myServerCfg.IP, myServerCfg.Port, myServerCfg.User, myServerCfg.PassWd);
- //配置数据存储路径
- UserPath = GetLogSavePath(Directory.GetCurrentDirectory() + "\\UserPath");
- ImportForm1.LocalCfgFilePath = UserPath + "\\cfg\\";
- ImportForm1.LogSavePath = UserPath+ "\\walkTest\\";
- CheckForm1.LocalCfgFilePath = UserPath + "\\cfg\\";
- CheckForm1.LogSavePath = UserPath + "\\qcTest\\";
- ManageForm1.FilePath = UserPath + "\\cfg\\";
- ExportForm1.FilePath = UserPath + "\\cfg\\";
- //检查本地文件夹
- if (!Directory.Exists(ImportForm1.LocalCfgFilePath))
- Directory.CreateDirectory(ImportForm1.LocalCfgFilePath);
- if (!Directory.Exists(ImportForm1.LogSavePath))
- Directory.CreateDirectory(ImportForm1.LogSavePath);
- if (!Directory.Exists(CheckForm1.LocalCfgFilePath))
- Directory.CreateDirectory(CheckForm1.LocalCfgFilePath);
- if (!Directory.Exists(CheckForm1.LogSavePath))
- Directory.CreateDirectory(CheckForm1.LogSavePath);
- if (!Directory.Exists(ManageForm1.FilePath))
- Directory.CreateDirectory(ManageForm1.FilePath);
- if (!Directory.Exists(ExportForm1.FilePath))
- Directory.CreateDirectory(ExportForm1.FilePath);
- //创建线程,定时检测网络连接状态
- Thread th = new Thread(NetworkCheck);
- th.IsBackground = true;
- th.Start();
- }
- //读取数据存储路径
- string GetLogSavePath(string fileName)
- {
- string result = "";
- try
- {
- //加载配置文件
- StreamReader objReader = new StreamReader(fileName);
- string sLine = "";
- List<string> array_CfgInfo = new List<string>();
- while (sLine != null)
- {
- sLine = objReader.ReadLine();
- array_CfgInfo.Add(sLine);
- }
- objReader.Close();
- result = array_CfgInfo[0].Substring(array_CfgInfo[0].LastIndexOf("=") + 1);
- if (result == "")
- {
- MessageBox.Show("文件加载失败,采用默认路径");
- result = Directory.GetCurrentDirectory();
- }
- }
- catch(Exception e)
- {
- MessageBox.Show("文件加载失败,采用默认路径",e.Message);
- result = Directory.GetCurrentDirectory();
- }
- return result;
- }
- //检查网络状态线程
- private void NetworkCheck()
- {
- //初始化第一次连接FTP服务器
- bool Result = myFtp.CheckFtp();
- this.Invoke((EventHandler)(delegate
- {
- if (Result == true)//服务器连接成功
- {
- label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
- label_NetStatus.ForeColor = Color.Green;
- ImportForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
- ImportForm1.label_NetStatus.BackColor = Color.Green;
- CheckForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
- CheckForm1.label_NetStatus.BackColor = Color.Green;
- if (myServerCfg.Local == "DISABLE")//不允许本地文件
- {
- CheckForm1.checkBox_LocalFile.Checked = false;
- CheckForm1.checkBox_LocalFile.Enabled = false;
- ImportForm1.checkBox_LocalFile.Checked = false;
- ImportForm1.checkBox_LocalFile.Enabled = false;
- }
- else//允许本地文件,默认在线模式
- {
- CheckForm1.checkBox_LocalFile.Checked = false;
- CheckForm1.checkBox_LocalFile.Enabled = true;
- ImportForm1.checkBox_LocalFile.Checked = false;
- ImportForm1.checkBox_LocalFile.Enabled = true;
- }
- myFtp.IsNetConnected = true;
- }
- else//服务器连接失败
- {
- label_NetStatus.Text = "网络连接失败";
- label_NetStatus.ForeColor = Color.Red;
- ImportForm1.label_NetStatus.Text = "网络连接失败";
- ImportForm1.label_NetStatus.BackColor = Color.Red;
- CheckForm1.label_NetStatus.Text = "网络连接失败";
- CheckForm1.label_NetStatus.BackColor = Color.Red;
- if (myServerCfg.Local == "DISABLE")//不允许本地文件
- {
- CheckForm1.checkBox_LocalFile.Checked = false;
- CheckForm1.checkBox_LocalFile.Enabled = false;
- ImportForm1.checkBox_LocalFile.Checked = false;
- ImportForm1.checkBox_LocalFile.Enabled = false;
- }
- else//允许本地文件
- {
- CheckForm1.checkBox_LocalFile.Checked = true;
- CheckForm1.checkBox_LocalFile.Enabled = false;
- ImportForm1.checkBox_LocalFile.Checked = true;
- ImportForm1.checkBox_LocalFile.Enabled = false;
- }
- myFtp.IsNetConnected = false;
- if (MessageBox.Show("网络连接失败,是否继续?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.No)
- {
- this.Close();
- }
- }
- button_Export.Enabled = true;
- button_Import.Enabled = true;
- button_Mang.Enabled = true;
- button_Check.Enabled = true;
- }));
- //创建定时器,定时10s检查网络
- System.Timers.Timer timer_CheckNet = new System.Timers.Timer();
- timer_CheckNet.Enabled = true;
- timer_CheckNet.Interval = 10000;
- timer_CheckNet.Elapsed += new System.Timers.ElapsedEventHandler(this.timer_CheckNet_Tick);
- timer_CheckNet.Start();
- while (true)
- {
- Thread.Sleep(1);
- }
- }
- //线程中定时任务,检查网络状态
- private void timer_CheckNet_Tick(object sender, EventArgs e)
- {
- //连接FTP服务器
- bool Result = myFtp.CheckFtp();
- this.Invoke((EventHandler)(delegate
- {
- if (Result == true)//服务器连接成功
- {
- label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
- label_NetStatus.ForeColor = Color.Green;
- ImportForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
- ImportForm1.label_NetStatus.BackColor = Color.Green;
- CheckForm1.label_NetStatus.Text = myServerCfg.ServerName + " | " + myServerCfg.IP + " | " + "连接成功";
- CheckForm1.label_NetStatus.BackColor = Color.Green;
- if (myServerCfg.Local == "DISABLE")//不允许本地文件
- {
- CheckForm1.checkBox_LocalFile.Enabled = false;
- ImportForm1.checkBox_LocalFile.Enabled = false;
- }
- else//允许本地文件
- {
- CheckForm1.checkBox_LocalFile.Enabled = true;
- ImportForm1.checkBox_LocalFile.Enabled = true;
- }
-
- myFtp.IsNetConnected = true;
- //定时与服务器同步文件
- #if false
- //cfg
- PathSyncWithServer("/cfg");
- //qcTest/errorlog
- PathSyncWithServer("/qcTest/errorlog");
- //qcTest/log
- PathSyncWithServer("/qcTest/log");
- //walkTest/errorlog
- PathSyncWithServer("/walkTest/errorlog");
- //walkTest/log
- PathSyncWithServer("/walkTest/log");
- #endif
- }
- else//服务器连接失败
- {
- label_NetStatus.Text = "网络连接失败";
- label_NetStatus.ForeColor = Color.Red;
- ImportForm1.label_NetStatus.Text = "网络连接失败";
- ImportForm1.label_NetStatus.BackColor = Color.Red;
- ImportForm1.checkBox_LocalFile.Checked = true;
- ImportForm1.checkBox_LocalFile.Enabled = false;
- CheckForm1.label_NetStatus.Text = "网络连接失败";
- CheckForm1.label_NetStatus.BackColor = Color.Red;
- CheckForm1.checkBox_LocalFile.Checked = true;
- CheckForm1.checkBox_LocalFile.Enabled = false;
- myFtp.IsNetConnected = false;
- }
- }));
- }
- /// <summary>
- /// 获取本地和远端目录名差集,本地有但远端没有
- /// </summary>
- /// <param name="localPath">本地路径</param>
- /// <param name="remotePath">远端路径</param>
- /// <returns>差集文件夹名称集合</returns>
- private string[] FiltDiffDirectory(string localPath, string remotePath)
- {
- try
- {
- string[] Local = System.IO.Directory.GetDirectories(localPath);
- for (int i = 0; i < Local.Length; i++)
- Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
- string[] Remote = StartForm.myFtp.GetDirectoryList(remotePath);
- for (int i = 0; i < Remote.Length; i++)
- Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
- return Local.Except(Remote).ToList().ToArray();
- }
- catch (Exception ex)
- {
- throw new Exception("同步失败,原因: " + ex.Message);
- }
- }
- /// <summary>
- /// 获取本地和远端文件夹下目录名交集
- /// </summary>
- /// <param name="localPath"></param>
- /// <param name="remotePath"></param>
- /// <returns></returns>
- private string[] FiltCommonDirectory(string localPath, string remotePath)
- {
- try
- {
- string[] Local = System.IO.Directory.GetDirectories(localPath);
- for (int i = 0; i < Local.Length; i++)
- Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
- string[] Remote = StartForm.myFtp.GetDirectoryList(remotePath);
- for (int i = 0; i < Remote.Length; i++)
- Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
- return Local.Intersect(Remote).ToList().ToArray();
- }
- catch (Exception ex)
- {
- throw new Exception("同步失败,原因: " + ex.Message);
- }
- }
- /// <summary>
- /// 获取本地和远端文件名差集,本地存在而远端不存在
- /// </summary>
- /// <param name="localPath">本地路径</param>
- /// <param name="remotePath">远端路径</param>
- /// <returns>差集文件名集合</returns>
- private string[] FiltDiffFiles(string localPath, string remotePath)
- {
- try
- {
- string[] Local = System.IO.Directory.GetFiles(localPath);
- for (int i = 0; i < Local.Length; i++)
- Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
- string[] Remote = StartForm.myFtp.GetFileNameList(remotePath);
- for (int i = 0; i < Remote.Length; i++)
- Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
- return Local.Except(Remote).ToList().ToArray();
- }
- catch (Exception ex)
- {
- throw new Exception("同步失败,原因: " + ex.Message);
- }
- }
- /// <summary>
- /// 获取本地和远端文件名交集
- /// </summary>
- /// <param name="localPath"></param>
- /// <param name="remotePath"></param>
- /// <returns></returns>
- private string[] FiltCommonFiles(string localPath, string remotePath)
- {
- try
- {
- string[] Local = System.IO.Directory.GetFiles(localPath);
- for (int i = 0; i < Local.Length; i++)
- Local[i] = Local[i].Substring(Local[i].LastIndexOf("\\") + 1);
- string[] Remote = StartForm.myFtp.GetFileNameList(remotePath);
- for (int i = 0; i < Remote.Length; i++)
- Remote[i] = Remote[i].Substring(Remote[i].LastIndexOf("/") + 1);
- return Local.Intersect(Remote).ToList().ToArray();
- }
- catch (Exception ex)
- {
- throw new Exception("同步失败,原因: " + ex.Message);
- }
- }
- /// <summary>
- /// 同步本地和远端文件夹
- /// 本地有、远端无的文件夹,上传该目录下本地所有文件
- /// 本地有和远端有的文件夹,上传本地有但远端没有的文件
- /// </summary>
- /// <param name="PathName"></param>
- /// <returns></returns>
- private void PathSyncWithServer(string PathName)
- {
- List<string> pathResult = null;
- //获取本地和远端的目录差集
- pathResult = FiltDiffDirectory(UserPath + PathName, myServerCfg.RootPath + PathName).ToList();
- if (pathResult.Count != 0)
- {
- foreach (string path in pathResult)
- {
- //远端建立文件夹
- myFtp.MakeDir(myServerCfg.RootPath + PathName + "/" + path);
- //获取待上传文件列表
- string[] fileList = Directory.GetFiles(UserPath + PathName + "\\" + path);
- for (int i = 0; i < fileList.Length; i++)
- fileList[i] = fileList[i].Substring(fileList[i].LastIndexOf("\\") + 1);
- //上传文件
- if (fileList.Length != 0)
- {
- foreach (string file in fileList)
- {
- myFtp.UploadFile(UserPath + PathName + "\\" + path + "\\" + file, myServerCfg.RootPath + PathName + "/" + path);
- }
- }
- }
- }
- //获取本地和远端的目录交集
- pathResult = FiltCommonDirectory(UserPath + PathName, myServerCfg.RootPath + PathName).ToList();
- if (pathResult.Count != 0)
- {
- foreach (string path in pathResult)
- {
- //获取该目录本地和远端文件名的差集,本地有但远端无
- string[] fileList = FiltDiffFiles(UserPath + PathName + "\\" + path, myServerCfg.RootPath + PathName + "/" + path);
- //上传文件
- if (fileList.Length != 0)
- {
- foreach (string file in fileList)
- {
- myFtp.UploadFile(UserPath + PathName + "\\" + path + "\\" + file, myServerCfg.RootPath + PathName + "/" + path);
- }
- }
- }
- }
- }
-
- private void button_Import_Click(object sender, EventArgs e)
- {
- if (myServerCfg.Local == "DISABLE")//不允许本地文件
- {
- if (myFtp.IsNetConnected == false)
- {
- MessageBox.Show("请检查网络,或联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- }
- CheckForm1.timer1.Enabled = false;
- ImportForm1.ShowDialog();
- }
- private void button_Export_Click(object sender, EventArgs e)
- {
- EnterForm1.ShowDialog();
- if ((EnterForm1.textBox_User.Text == "PT") && (EnterForm1.textBox_Passwd.Text == "123456"))
- {
- ExportForm1.ShowDialog();
- }
- else
- {
- MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
-
- private void button_Mang_Click(object sender, EventArgs e)
- {
- EnterForm1.ShowDialog();
- if ((EnterForm1.textBox_User.Text == "PT") && (EnterForm1.textBox_Passwd.Text == "123456"))
- {
- ManageForm1.ShowDialog();
- }
- else
- {
- MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- private void button_Check_Click(object sender, EventArgs e)
- {
- if (myServerCfg.Local == "DISABLE")//不允许本地文件
- {
- if (myFtp.IsNetConnected == false)
- {
- MessageBox.Show("请检查网络,或联系管理员!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- }
- ImportForm1.timer1.Enabled = false;
- CheckForm1.ShowDialog();
- }
- private void StartForm_Load(object sender, EventArgs e)
- {
- }
- private void StartForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- }
- }
- }
|