using System;
using System.Data;
using System.Linq;
using System.Net;
using System.ServiceModel;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using System.Xml.Linq;
using Curtain.DataAccess;
using Curtain.Log;
using Dongke.IBOSS.PRD.Basics.BaseResources;
using Dongke.IBOSS.PRD.Basics.DataAccess;
using Dongke.IBOSS.PRD.Basics.Library;
using Dongke.IBOSS.PRD.WCF.Services;
namespace Dongke.IBOSS.PRD.WCF.Hosting
{
public partial class ConfigMain : Form
{
private bool _isClose = false;
private SynchronizationContext _syncContext;
private string _formTitle = "[{0}]" + Constant.M_SYSTEM_NAME + "服务器端配置及服务-{1}";
public ServiceHostCollection ServiceHosts
{
get;
private set;
}
public ConfigMain()
{
InitializeComponent();
txtExePath.Text = System.AppDomain.CurrentDomain.BaseDirectory;
_syncContext = SynchronizationContext.Current;
//ServiceInvoker.ServiceDoingEvent += DKIMSSWCFService_ServiceDoingEvent;
//ServiceInvoker.ServiceDoneEvent += DKIMSSWCFService_ServiceDoneEvent;
//ServiceInvoker.ServiceExceptionEvent += DKIMSSWCFService_ServiceExceptionEvent;
Logger.DefaultLogDirectory = "Log";
// 启动内存回收(每小时)
timer1.Interval = 1000 * 60 * 60;
timer1.Start();
}
#region 事件
private void ConfigMain_Load(object sender, EventArgs e)
{
try
{
GetPublishConfig();
GetProgramSetting();
// 获取数据库配置信息
GetDBSetting();
GetLocalIPAddress();
GetInternetIPAddress();
this.butSopt.Enabled = false;
this.tsmiStop.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
///
/// 启动服务方法
///
///
///
private void butStart_Click(object sender, EventArgs e)
{
try
{
Application.DoEvents();
this.txtLogInfo.Text = "服务开始准备启动!" + Environment.NewLine;
IsCheckInfo();
SetProgramSetting();
WCFStart();
GetLocalIPAddress();
GetInternetIPAddress();
MessageBox.Show("服务器WCF服务启动成功。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.butStart.Enabled = false;
this.butSopt.Enabled = true;
this.tsmiStart.Enabled = false;
this.tsmiStop.Enabled = true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
}
}
///
/// 服务重新启动方法
///
///
///
private void butReStart_Click(object sender, EventArgs e)
{
try
{
Application.DoEvents();
this.txtLogInfo.Text = "服务开始准备重新启动。" + Environment.NewLine;
IsCheckInfo();
SetProgramSetting();
WCFStart();
GetLocalIPAddress();
GetInternetIPAddress();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
}
}
///
/// 服务停止方法
///
///
///
private void butSopt_Click(object sender, EventArgs e)
{
try
{
Application.DoEvents();
this.txtLogInfo.Text = "服务开始准备关闭。" + Environment.NewLine;
WCFStop();
MessageBox.Show("服务器WCF服务停止成功。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
this.butStart.Enabled = true;
this.butSopt.Enabled = false;
this.tsmiStart.Enabled = true;
this.tsmiStop.Enabled = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
}
}
///
/// 显示日志方法
///
///
///
private void butShowLog_Click(object sender, EventArgs e)
{
try
{
var logBity = LogFileOperation.ReadFile(ConfigConst.LogFileName);
this.txtLogInfo.Text = System.Text.Encoding.UTF8.GetString(logBity);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
///
/// 清空日志方法
///
///
///
private void butClearLog_Click(object sender, EventArgs e)
{
try
{
this.txtLogInfo.Clear();
LogFileOperation.DeleteLogFile(ConfigConst.LogFileName);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
///
/// 关闭窗体
///
///
///
private void butClose_Click(object sender, EventArgs e)
{
//DialogResult exitDialogResult = MessageBox.Show("退出本服务程序,服务器的WCF服务将会停止。\r\n确定要退出本服务程序吗?",
// this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
//if (exitDialogResult == DialogResult.Yes)
//{
// if (this.ServiceHosts != null)
// {
// DialogResult dialogResult = MessageBox.Show("WCF服务正在运行中,是否强行停止?",
// this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
// if (dialogResult == DialogResult.Yes)
// {
// this.WCFStop();
// }
// else
// {
// return;
// }
// }
// this._isClose = true;
// this.Close();
//}
//else
//{
// return;
//}
if (this.ServiceHosts != null)
{
DialogResult dialogResult = MessageBox.Show("WCF服务正在运行中,退出后服务将会停止。\r\n确定要退出本服务程序?",
this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
if (dialogResult == DialogResult.OK)
{
this.WCFStop();
this._isClose = true;
this.Close();
}
else
{
return;
}
}
else
{
DialogResult exitDialogResult = MessageBox.Show("确定要退出本服务程序?",
this.Text, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
if (exitDialogResult == DialogResult.OK)
{
this._isClose = true;
this.Close();
}
else
{
return;
}
}
}
///
/// 窗体关闭验证
///
///
///
private void ConfigMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (this._isClose)
{
if (this.ServiceHosts != null)
{
e.Cancel = true;
this._isClose = false;
MessageBox.Show("请先停止WCF服务,然后再退出本程序。");
this.Visible = true;
this.WindowState = FormWindowState.Normal;
}
else
{
e.Cancel = false;
this.notifyIcon1.Visible = false;
}
}
else
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.Visible = false;
}
}
///
/// 保存数据库配置
///
///
///
private void btnDBSave_Click(object sender, EventArgs e)
{
try
{
INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "DBIP", this.txtDBIP.Text.Trim());
INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "DBPort", this.txtDBPort.Text.Trim());
INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "SID", this.txtSID.Text.Trim());
INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "DBUser", this.txtUser.Text.Trim());
//INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "DBPassword", this.txtPassword.Text.Trim());
try
{
string password = Encryption.EncryptDES(this.txtPassword.Text.Trim(), Constant.S_ENCRYPTION_KEY);
INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("DBSetting", "DBPassword", password);
}
catch
{
}
// 更改连接串
DataManager.ConnectionString = string.Format(DataManager.ConnectionStringFormat,
this.txtDBIP.Text.Trim(),
this.txtDBPort.Text.Trim(),
this.txtSID.Text.Trim(),
this.txtUser.Text.Trim(),
this.txtPassword.Text.Trim()
);
DataAccess.DefaultDataBaseType = Curtain.DataAccess.DataBaseType.Oracle;
DataAccess.DefaultConnectionString = DataManager.ConnectionString;
MessageBox.Show("数据库配置保存成功。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show("数据库配置保存失败。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
}
}
///
/// 连接测试
///
///
///
private void btnDBTest_Click(object sender, EventArgs e)
{
#region 校验
if (string.IsNullOrEmpty(this.txtDBIP.Text.Trim()))
{
MessageBox.Show("请输入数据库IP地址!");
this.txtDBIP.Focus();
return;
}
else if (string.IsNullOrEmpty(this.txtDBPort.Text.Trim()))
{
MessageBox.Show("请输入数据库端口号!");
this.txtDBPort.Focus();
return;
}
else if (string.IsNullOrEmpty(this.txtSID.Text.Trim()))
{
MessageBox.Show("请输入数据库SID!");
this.txtSID.Focus();
return;
}
else if (string.IsNullOrEmpty(this.txtUser.Text.Trim()))
{
MessageBox.Show("请输入用户名!");
this.txtUser.Focus();
return;
}
else if (string.IsNullOrEmpty(this.txtPassword.Text.Trim()))
{
MessageBox.Show("请输入密码!");
this.txtPassword.Focus();
return;
}
//string strIpRegEdp = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
//Regex regEdpIP = new Regex(strIpRegEdp, RegexOptions.None);
//if (!regEdpIP.IsMatch(this.txtDBIP.Text.Trim()))
//{
// MessageBox.Show("数据库IP地址不符合IP规则!");
// this.txtDBIP.Focus();
// return;
//}
#endregion
// 连接串
string conStr = string.Format(DataManager.ConnectionStringFormat,
this.txtDBIP.Text.Trim(),
this.txtDBPort.Text.Trim(),
this.txtSID.Text.Trim(),
this.txtUser.Text.Trim(),
this.txtPassword.Text.Trim()
);
//IDBConnection oracleConn = ClsDbFactory.CreateDBConnection(DataBaseType.ORACLE, conStr);
//try
//{
// oracleConn.Open();
// string sql = "select 1 from dual";
// DataSet returnDataSet = oracleConn.GetSqlResultToDs(sql);
// DialogResult dialogResult = MessageBox.Show("数据库连接测试成功。\r\n是否保存数据库配置?",
// this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
// if (DialogResult.Yes == dialogResult)
// {
// this.btnDBSave_Click(sender, e);
// }
//}
//catch (Exception ex)
//{
// MessageBox.Show("数据库连接测试失败。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
// LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
//}
//finally
//{
// if (oracleConn.ConnState == ConnectionState.Open)
// {
// oracleConn.Close();
// }
//}
IDataAccess dataAccess = null;
try
{
dataAccess = DataAccess.Create(Curtain.DataAccess.DataBaseType.Oracle, conStr);
dataAccess.ExecuteScalar("select sysdate from dual");
DialogResult dialogResult = MessageBox.Show("数据库连接测试成功。\r\n是否保存数据库配置?",
this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (DialogResult.Yes == dialogResult)
{
this.btnDBSave_Click(sender, e);
}
}
catch (Exception ex)
{
MessageBox.Show("数据库连接测试失败。", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Warning);
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
}
finally
{
dataAccess?.Close();
}
}
#endregion
#region 自定义方法
///
/// 获取系统配置相关信息
///
public void GetPublishConfig()
{
try
{
ConfigConst.StartupPath = System.Windows.Forms.Application.StartupPath;
#region 获取对应的配置信息
ConfigConst.LogControl = false;
//if (System.Configuration.ConfigurationManager.AppSettings["LogControl"] != null)
//{
// ConfigConst.LogControl = System.Configuration.ConfigurationManager.AppSettings["LogControl"].ToString().Trim().ToUpper().Equals("TRUE") ? true : false;
ConfigConst.LogControl = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("LogSetting", "LogControl").ToUpper().Equals("TRUE") ? true : false;
//}
///日志文件
//if (System.Configuration.ConfigurationManager.AppSettings["LogFileName"] != null)
//{
// ConfigConst.LogFileName = System.Configuration.ConfigurationManager.AppSettings["LogFileName"].Trim();
ConfigConst.LogFileName = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("LogSetting", "LogFileName");
//}
//配置文件名称
//if (System.Configuration.ConfigurationManager.AppSettings["ConfigFileName"] != null)
//{
// ConfigConst.ConfigFileName = System.Configuration.ConfigurationManager.AppSettings["ConfigFileName"].Trim();
//}
#endregion
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取程序所需要的配置信息并显示在对应的控件中
///
public void GetProgramSetting()
{
this.txtWcfIp.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("WCFSetting", "WCFIP");//System.Configuration.ConfigurationManager.AppSettings["WCFIP"].Trim();
this.txtWcfPort.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("WCFSetting", "WCFPort");//System.Configuration.ConfigurationManager.AppSettings["WCFPort"].Trim();
this.txtServerName.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("WCFSetting", "ServerName");
string v = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("VersionSetting", "ClientVersion");
this.Text = string.Format(this._formTitle, this.txtServerName.Text, v);
this.notifyIcon1.Text = "【" + this.txtServerName.Text + "】" + Constant.M_SYSTEM_NAME + "-" + v;//this.Text;
}
///
/// 根据XML节点名称获取对应的数据
///
///
///
///
public string GetXElementName(XDocument pXDocument, string pNodeName)
{
try
{
XElement xelemnt = (from p in pXDocument.Root.Elements() where p.Name == pNodeName select p).FirstOrDefault();
if (xelemnt == null)
return "";
return xelemnt.Value;
}
catch (Exception ex)
{
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
throw ex;
}
}
///
/// 获取XML中对应的节点
///
///
///
///
public XElement GetFirstNode(XDocument pXDocument, string pNodeName)
{
try
{
XElement xelemnt = (from p in pXDocument.Root.Elements() where p.Name == pNodeName select p).FirstOrDefault();
return xelemnt;
}
catch (Exception ex)
{
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
throw ex;
}
}
///
/// 保存数据的验证处理
///
public void IsCheckInfo()
{
string strWcfIp = this.txtWcfIp.Text.Trim();
if (string.IsNullOrEmpty(strWcfIp))
{
throw new Exception("WCF服务的主机IP地址不能为空。");
}
string strIpRegEdp = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$";
Regex regEdpIP = new Regex(strIpRegEdp, RegexOptions.None);
if (!regEdpIP.IsMatch(strWcfIp))
{
throw new Exception("WCF服务的主机IP地址不符合IP地址规则。");
}
string strWcfPort = this.txtWcfPort.Text.Trim();
if (string.IsNullOrEmpty(strWcfPort))
{
throw new Exception("WCF服务的主机端口号不能为空。");
}
var strPortRegEdp = @"/^[1-9]$|(^[1-9][0-9]$)|(^[1-9][0-9][0-9]$)|(^[1-9][0-9][0-9][0-9]$)|(^[1-6][0-5][0-5][0-3][0-5]$)/";
Regex regEdpPort = new Regex(strPortRegEdp, RegexOptions.None);
if (!regEdpPort.IsMatch(strWcfPort))
{
throw new Exception("WCF服务的主机端口号不符合端口号规则。");
}
}
///
/// 保存设置到XML文件
///
public void SetProgramSetting()
{
INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("WCFSetting", "WCFIP", this.txtWcfIp.Text.Trim());
INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("WCFSetting", "WCFPort", this.txtWcfPort.Text.Trim());
}
///
/// 添加数据到XML集合中
///
///
///
///
public void SetXElementName(XDocument pXDocument, string pNodeName, string pNameValue)
{
try
{
XElement xmlElem = GetFirstNode(pXDocument, pNodeName);
if (xmlElem != null)
{
xmlElem.Value = pNameValue;
}
else
{
XElement tempXElem = new XElement(pNodeName);
tempXElem.Value = pNameValue;
pXDocument.Root.Add(tempXElem);
}
}
catch (Exception ex)
{
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
throw ex;
}
}
public void WCFStart()
{
try
{
if (this.ServiceHosts != null)
{
this.ServiceHosts.Dispose();
this.ServiceHosts = null;
}
this.ServiceHosts = new ServiceHostCollection();
this.ServiceHosts.IP = this.txtWcfIp.Text.Trim();
this.ServiceHosts.Port = this.txtWcfPort.Text.Trim();
this.ServiceHosts.Init();
foreach (ServiceHost host in ServiceHosts)
{
host.Opening += host_Opening;
host.Opened += host_Opened;
host.Closing += host_Closing;
host.Closed += host_Closed;
}
ServiceHostOpen();
}
catch (Exception ex)
{
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
throw ex;
}
}
public void ServiceHostOpen()
{
try
{
foreach (ServiceHost host in ServiceHosts)
{
//
//可以增加对控制
//
host.Open();
}
}
catch (Exception ex)
{
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
throw ex;
}
}
public void WCFStop()
{
try
{
if (this.ServiceHosts != null)
{
this.ServiceHosts.Dispose();
this.ServiceHosts = null;
}
}
catch (Exception ex)
{
LogFileOperation.Error(ConfigConst.LogFileName, ex.ToString());
throw ex;
}
}
void host_Opening(object sender, EventArgs e)
{
SendOrPostCallback callback = delegate(object state)
{
string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "打开中......";
LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
this.txtLogInfo.Text += strInfo + Environment.NewLine;
};
_syncContext.Post(callback, null);
}
void host_Opened(object sender, EventArgs e)
{
SendOrPostCallback callback = delegate(object state)
{
string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "打开完毕。";
LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
this.txtLogInfo.Text += strInfo + Environment.NewLine;
};
_syncContext.Post(callback, null);
}
void host_Closed(object sender, EventArgs e)
{
SendOrPostCallback callback = delegate(object state)
{
string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭完毕。";
LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
this.txtLogInfo.Text += strInfo + Environment.NewLine;
};
_syncContext.Post(callback, null);
}
void host_Closing(object sender, EventArgs e)
{
SendOrPostCallback callback = delegate(object state)
{
string strInfo = "服务" + (sender as ServiceHost).BaseAddresses[0] + "关闭中......";
LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
this.txtLogInfo.Text += strInfo + Environment.NewLine;
};
_syncContext.Post(callback, null);
}
///
/// 服务开始
///
///
///
void DKIMSSWCFService_ServiceDoingEvent(object sender, ServiceEventArgs e)
{
SendOrPostCallback callback = delegate(object state)
{
string strInfo = "服务" + e.MethodName + " 服务开始。 ";
LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
};
_syncContext.Post(callback, null);
}
///
/// 服务结束
///
///
///
void DKIMSSWCFService_ServiceDoneEvent(object sender, ServiceEventArgs e)
{
SendOrPostCallback callback = delegate(object state)
{
string strInfo = "服务" + e.MethodName + " 服务结束。 ";
LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
};
_syncContext.Post(callback, null);
}
///
/// 服务异常
///
///
///
void DKIMSSWCFService_ServiceExceptionEvent(object sender, ServiceEventArgs e)
{
//_syncContext.Post(state => txtDoing.Text += e.MethodName + " has Exception ... " + "\r\n", null);
SendOrPostCallback callback = delegate(object state)
{
string strInfo = "服务" + e.MethodName + " 服务异常。 ";
LogFileOperation.Fatal(ConfigConst.LogFileName, strInfo);
};
_syncContext.Post(callback, null);
}
///
/// 获取数据库配置
///
private void GetDBSetting()
{
this.txtDBIP.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "DBIP");
this.txtDBPort.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "DBPort");
this.txtSID.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "SID");
this.txtUser.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "DBUser");
this.txtPassword.Text = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("DBSetting", "DBPassword");
//try
//{
// this.txtPassword.Text = Encryption.DecryptDES(this.txtPassword.Text.Trim(), Constant.SECRET_KEY);
//}
//catch
//{
//}
string password = null;
try
{
password = Encryption.DecryptDES(this.txtPassword.Text.Trim(), Constant.S_ENCRYPTION_KEY);
}
catch
{
}
this.txtPassword.Text = password;
// 更改连接串
DataManager.ConnectionString = string.Format(DataManager.ConnectionStringFormat,
this.txtDBIP.Text.Trim(),
this.txtDBPort.Text.Trim(),
this.txtSID.Text.Trim(),
this.txtUser.Text.Trim(),
this.txtPassword.Text.Trim()
);
DataAccess.DefaultDataBaseType = Curtain.DataAccess.DataBaseType.Oracle;
DataAccess.DefaultConnectionString = DataManager.ConnectionString;
}
///
/// 内网地址
///
private void GetLocalIPAddress()
{
string localIP = null;
try
{
IPHostEntry ipHost = System.Net.Dns.GetHostEntry(Dns.GetHostName());// Dns.Resolve(Dns.GetHostName()); ;
localIP = "unknown";
foreach (var item in ipHost.AddressList)
{
localIP = item.ToString();
//判断IP是否合法
if (System.Text.RegularExpressions.Regex.IsMatch(localIP, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
{
break;
}
}
this.lblLocalIPAddress.Text = localIP;
}
catch
{
this.lblLocalIPAddress.Text = "unknown";
}
}
///
/// 外网地址
///
private void GetInternetIPAddress()
{
//string internetAddress = "";
//try
//{
// using (WebClient webClient = new WebClient())
// {
// internetAddress = webClient.DownloadString("http://www.coridc.com/ip");//从外部网页获得IP地址
// //判断IP是否合法
// if (!System.Text.RegularExpressions.Regex.IsMatch(internetAddress, "[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))
// {
// internetAddress = webClient.DownloadString("http://fw.qq.com/ipaddress");//从腾讯提供的API中获得IP地址
// }
// }
// this.lblInternetIPAddress.Text = internetAddress;
//}
//catch
//{
// this.lblInternetIPAddress.Text = "unknown";
//}
this.lblInternetIPAddress.Text = "unknown";
}
#endregion
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
if (!this.Visible)
{
this.Visible = true;
//this.WindowState = FormWindowState.Normal;
}
this.WindowState = FormWindowState.Normal;
this.Activate();
}
private void 回收ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
GC.Collect();
}
catch
{
}
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
GC.Collect();
}
catch
{
}
}
private void btnServerName_Click(object sender, EventArgs e)
{
INIUtility.Instance(INIUtility.IniFile.Config).WriteIniData("WCFSetting", "ServerName", this.txtServerName.Text.Trim());
string v = INIUtility.Instance(INIUtility.IniFile.Config).ReadIniData("VersionSetting", "ClientVersion");
this.Text = string.Format(this._formTitle, this.txtServerName.Text, v);
this.notifyIcon1.Text = "【" + this.txtServerName.Text + "】" + Constant.M_SYSTEM_NAME + "-" + v;//this.Text;
}
private void btnOpen_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(txtExePath.Text);
}
private void button1_Click(object sender, EventArgs e)
{
F_LicCode lc = new F_LicCode(this.txtSID.Text);
lc.ShowDialog();
}
}
}