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\\"; ManageForm1.textBox_LocalPath.Text = UserPath; //检查本地文件夹 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 array_CfgInfo = new List(); 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; } //检查最新版本号,与当前版本对比 string VerInfo = ""; VerInfo = StartForm.myFtp.GetFileNameList(StartForm.myServerCfg.RootPath + "/version/")[0]; string VerInfoCur = label_Ver.Text; if (VerInfoCur.Contains(VerInfo) == false) { MessageBox.Show("最新版本为" + VerInfo + ",请联系管理员!"); this.Close(); } 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; } })); } /// /// 获取本地和远端目录名差集,本地有但远端没有 /// /// 本地路径 /// 远端路径 /// 差集文件夹名称集合 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); } } /// /// 获取本地和远端文件夹下目录名交集 /// /// /// /// 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); } } /// /// 获取本地和远端文件名差集,本地存在而远端不存在 /// /// 本地路径 /// 远端路径 /// 差集文件名集合 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); } } /// /// 获取本地和远端文件名交集 /// /// /// /// 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); } } /// /// 同步本地和远端文件夹 /// 本地有、远端无的文件夹,上传该目录下本地所有文件 /// 本地有和远端有的文件夹,上传本地有但远端没有的文件 /// /// /// private void PathSyncWithServer(string PathName) { List 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; } } //输入用户名 EnterForm1.ShowDialog(); try { if (EnterForm1.textBox_Passwd.Text == EnterForm1.UserAccount[EnterForm1.comboBox_User.Text])//检验模式所有用户支持 { CheckForm1.timer1.Enabled = false; if (EnterForm1.comboBox_User.Text == "武汉Wuhan") //武汉账户不使用本地文件,需要使用时采用工程账户登录 ImportForm1.checkBox_LocalFile.Enabled = false; ImportForm1.ShowDialog(); } else { MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception) { MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void button_Export_Click(object sender, EventArgs e) { EnterForm1.ShowDialog(); try { if(EnterForm1.textBox_Passwd.Text == EnterForm1.UserAccount[EnterForm1.comboBox_User.Text])//生成参数模式仅用户PT { if (EnterForm1.comboBox_User.Text == "工程Engineer") { ExportForm1.ShowDialog(); } else { MessageBox.Show("权限不支持", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception) { MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void button_Mang_Click(object sender, EventArgs e) { EnterForm1.ShowDialog(); try { if (EnterForm1.textBox_Passwd.Text == EnterForm1.UserAccount[EnterForm1.comboBox_User.Text])//本地文件管理仅支持PT用户 { if (EnterForm1.comboBox_User.Text == "工程Engineer") { ManageForm1.ShowDialog(); } else { MessageBox.Show("权限不支持", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { MessageBox.Show("密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception) { 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; } } //输入用户名 EnterForm1.ShowDialog(); try { if (EnterForm1.textBox_Passwd.Text == EnterForm1.UserAccount[EnterForm1.comboBox_User.Text])//所有用户支持 { ImportForm1.timer1.Enabled = false; if (EnterForm1.comboBox_User.Text == "武汉Wuhan") //武汉账户不使用本地文件,需要使用时采用工程账户登录 CheckForm1.checkBox_LocalFile.Enabled = false; CheckForm1.ShowDialog(); } else { MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception) { MessageBox.Show("用户不存在或密码错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } } private void StartForm_Load(object sender, EventArgs e) { } private void StartForm_FormClosing(object sender, FormClosingEventArgs e) { #if false //关闭前,删除本地所有配置文件 string path = StartForm.UserPath + "\\cfg\\"; ; try { DirectoryInfo dir = new DirectoryInfo(path); FileSystemInfo[] fileinfo = dir.GetFileSystemInfos(); //返回目录中所有文件和子目录 foreach (FileSystemInfo i in fileinfo) { if (i is DirectoryInfo) //判断是否文件夹 { DirectoryInfo subdir = new DirectoryInfo(i.FullName); subdir.Delete(true); //删除子目录和文件 } else { File.Delete(i.FullName); //删除指定文件 } } //MessageBox.Show("文件清理成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } catch (Exception) { MessageBox.Show("文件清理失败!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } #endif } } }