using DevExpress.XtraEditors; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Entity; using System.Drawing; using System.Linq; using System.Net.Sockets; using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Documents; using System.Windows.Forms; using CG.App.EFContext; namespace CG.App.UserControl { public partial class CtrlSysSettings : DevExpress.XtraEditors.XtraUserControl { public CtrlSysSettings() { InitializeComponent(); } private async void CtrlSysSettings_Load(object sender, EventArgs e) { try { txtIp.Properties.Items.Add("127.0.0.1"); string name = Dns.GetHostName(); IPAddress[] ipadrlist = Dns.GetHostAddresses(name); foreach (IPAddress ipa in ipadrlist) { if (ipa.AddressFamily == AddressFamily.InterNetwork) txtIp.Properties.Items.Add(ipa.ToString()); } } catch(Exception ex) { Serilog.Log.Error("获取本机IPv4地址出错", ex); } try { txtIp.SelectedIndex = 0; using (RHDWContext db = new RHDWContext()) { var res = await db.SysSetings.FirstOrDefaultAsync(); if (res != null) { this.txtIp.Text = res.ServerIp; this.txtPort.Text = res.Port.ToString(); } } } catch (Exception ex) { Serilog.Log.Error("加载配置信息异常", ex); XtraMessageBox.Show("加载配置信息异常"); } } private async void btnSave_Click(object sender, EventArgs e) { try { using (RHDWContext db = new RHDWContext()) { var res = await db.SysSetings.FirstOrDefaultAsync(); if (res == null) { res = new XzXdDw.App.Model.SysSetings() { ServerIp = txtIp.Text, Port = Convert.ToInt32(txtPort.Text), }; db.SysSetings.Add(res); } else { res.ServerIp = txtIp.Text; res.Port = Convert.ToInt32(txtPort.Text); } await db.SaveChangesAsync(); XtraMessageBox.Show("配置信息保存成功!"); } } catch (Exception ex) { Serilog.Log.Error("配置信息保存失败", ex); XtraMessageBox.Show("配置信息保存失败"); } } } }