123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- 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<string> array_CfgInfo = new List<string>();
- 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)
- {
- }
- }
- }
|