using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; using System.IO; 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\\"; //创建线程,定时检测网络连接状态 Thread th = new Thread(NetworkCheck); th.IsBackground = true; th.Start(); } //读取数据存储路径 string GetLogSavePath(string fileName) { string result = ""; //加载配置文件 StreamReader objReader = new StreamReader(fileName); string sLine = ""; List array_CfgInfo = new List(); while (sLine != null) { sLine = objReader.ReadLine(); array_CfgInfo.Add(sLine); } objReader.Close(); try { result = array_CfgInfo[1]; } catch(Exception e) { MessageBox.Show(e.Message); } return result; } //检查网络状态线程 private void NetworkCheck() { //初始化第一次连接FTP服务器 bool Result = myFtp.CheckFtp(); this.Invoke((EventHandler)(delegate { if (Result == true)//服务器连接成功 { label_NetStatus.Text = "网络连接成功"; label_NetStatus.ForeColor = Color.Green; ImportForm1.label_NetStatus.Text = "网络连接成功"; ImportForm1.label_NetStatus.BackColor = Color.Green; CheckForm1.label_NetStatus.Text = "网络连接成功"; 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; })); //创建定时器,定时3s检查网络 System.Timers.Timer timer_CheckNet = new System.Timers.Timer(); timer_CheckNet.Enabled = true; timer_CheckNet.Interval = 3000; 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 = "网络连接成功"; label_NetStatus.ForeColor = Color.Green; ImportForm1.label_NetStatus.Text = "网络连接成功"; ImportForm1.label_NetStatus.BackColor = Color.Green; CheckForm1.label_NetStatus.Text = "网络连接成功"; 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; } 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 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) { //显示编译时间 label_Ver.Text += System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location).ToString(); } private void StartForm_FormClosing(object sender, FormClosingEventArgs e) { } } }