using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Data.SqlClient; using DevComponents.DotNetBar; using System.Xml; using System.IO; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Threading; namespace Cyberpipe { public partial class FrmLogin : Office2007Form { public FrmLogin() { InitializeComponent(); } public string rolename = "浏览"; public string username = ""; public string department = ""; //登录界面 -- 配置文件的路径 public static string filename = Application.StartupPath + "\\login.xml"; /// <summary> /// 登录按钮事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { bool result = ValidateUser(); if (result) { username = txtUser.Text.Trim(); Utility.userName = txtUser.Text.Trim(); this.DialogResult = DialogResult.OK; this.Close(); } else { MessageBox.Show("用户名、密码不正确,请重新输入!", "提示"); txtUser.Clear(); textBoxPassWord.Clear(); } //if (ValiPort()) //{ // bool result = ValidateUser(); // if (result) // { // username = txtUser.Text; // this.DialogResult = DialogResult.OK; // this.Close(); // } //} } /// <summary> /// 关闭按钮事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button2_Click(object sender, EventArgs e) { this.Close(); } /// <summary> /// 验证端口号 /// </summary> /// <returns></returns> private bool ValiPort() { Ping p = new Ping();//创建Ping对象p PingReply pr = p.Send(Utility.localicenseserverip);//向指定IP或者主机名的计算机发送ICMP协议的ping数据包 if (pr.Status == IPStatus.Success)//如果ping成功 { try { TcpClient tcpc = new TcpClient(Utility.localicenseserverip, Utility.localicenseserverport);//对IP地址为"192.168.0.105"的计算机的1500端口提出连接申请 tcpc.Close(); return true; } catch (Exception ex) { LogError.PublishError(ex); MessageBox.Show("端口连接错误!" + ex.Message); return false; } } else { int times = 0;//重新连接次数; int count = 2;//设置尝试次数 while (times < count) { //Thread.Sleep(1000);//等待时间(方便测试的话,你可以改为1000) pr = p.Send(Utility.localicenseserverip); if (pr.Status == IPStatus.Success) { try { TcpClient tcpc = new TcpClient(Utility.localicenseserverip, Utility.localicenseserverport);//对IP地址为"192.168.0.105"的计算机的1500端口提出连接申请 tcpc.Close(); return true; } catch (Exception ex) { LogError.PublishError(ex); MessageBox.Show("端口连接错误!" + ex.Message); return false; } } else { times++; if (times < count) { continue; } else { MessageBox.Show("重新尝试连接失败"); return false; } } } return false; } } /// <summary> /// 验证用户名、密码 /// </summary> /// <returns></returns> private bool ValidateUser() { string passWord = Utility.MD5Encrypt(textBoxPassWord.Text.Trim()); string sql = "select * from casic_userstatus where \"username\"='" + txtUser.Text.Trim() + "' and \"password\"='" + passWord + "'"; DataTable dt = OledbHelper.QueryTable(sql); if (dt != null && dt.Rows.Count > 0) { WriteXML(); return true; } else { return false; } } private void FrmLogin_Load(object sender, EventArgs e) { Utility.SetParams(); ReadXML(); } /// <summary> /// 读取上一次登录的用户名和密码 /// </summary> private void ReadXML() { if (File.Exists(filename)) { if (!File.Exists(filename)) { return; } //初始化XML文档操作类 XmlDocument myDoc = new XmlDocument(); { //加载XML文件 try { myDoc.Load(filename); } catch (System.Exception e) { LogError.PublishError(e); return; } //搜索指定的节点 XmlNode serverRootNode = myDoc.SelectSingleNode("Params"); XmlNodeList nodes = null; if (serverRootNode != null) { nodes = serverRootNode.ChildNodes; } if (nodes != null) { foreach (System.Xml.XmlNode xn in nodes) { if (xn.Name == "username") { txtUser.Text = xn.InnerText.Trim(); } else if (xn.Name == "password") { textBoxPassWord.Text = xn.InnerText.Trim(); } else if (xn.Name == "isremember") { if (xn.InnerText.Trim() == "true") { checkBoxXRememberPassword.Checked = true; } else if (xn.InnerText.Trim() == "false") { checkBoxXRememberPassword.Checked = false; } } } } } } } /// <summary> /// 记录登录的用户名和密码 /// </summary> private void WriteXML() { if (!File.Exists(filename)) { return; } //初始化XML文档操作类 XmlDocument myDoc = new XmlDocument(); //加载XML文件 try { myDoc.Load(filename); } catch (System.Exception e) { LogError.PublishError(e); MessageBox.Show(e.Message, "提示"); return; } //搜索指定的节点 XmlNode serverRootNode = myDoc.SelectSingleNode("Params"); XmlNodeList nodes = null; if (serverRootNode != null) { nodes = serverRootNode.ChildNodes; } if (nodes != null) { foreach (System.Xml.XmlNode xn in nodes) { XmlElement xe = (XmlElement)xn; if (xe.Name == "username") { xe.InnerText = txtUser.Text.Trim(); } else if (xe.Name == "password") { if (checkBoxXRememberPassword.Checked) { xe.InnerText = textBoxPassWord.Text.Trim(); } else { xe.InnerText = ""; } } else if (xe.Name == "isremember") { if (checkBoxXRememberPassword.Checked) { xe.InnerText = "true"; } else { xe.InnerText = "false"; } } } } myDoc.Save(filename); } /// <summary> /// 关闭按钮事件处理 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } } }