CtrlSysSettings.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using DevExpress.XtraEditors;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Data.Entity;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Net.Sockets;
  10. using System.Net;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Documents;
  14. using System.Windows.Forms;
  15. using XdCxRhDW.App.EFContext;
  16. namespace XdCxRhDW.App.UserControl
  17. {
  18. public partial class CtrlSysSettings : DevExpress.XtraEditors.XtraUserControl
  19. {
  20. public CtrlSysSettings()
  21. {
  22. InitializeComponent();
  23. }
  24. private async void CtrlSysSettings_Load(object sender, EventArgs e)
  25. {
  26. try
  27. {
  28. txtIp.Properties.Items.Add("127.0.0.1");
  29. string name = Dns.GetHostName();
  30. IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
  31. foreach (IPAddress ipa in ipadrlist)
  32. {
  33. if (ipa.AddressFamily == AddressFamily.InterNetwork)
  34. txtIp.Properties.Items.Add(ipa.ToString());
  35. }
  36. }
  37. catch(Exception ex)
  38. {
  39. Serilog.Log.Error(ex,"获取本机IPv4地址出错");
  40. }
  41. try
  42. {
  43. txtIp.SelectedIndex = 0;
  44. using (RHDWContext db = new RHDWContext())
  45. {
  46. var res = await db.SysSetings.FirstOrDefaultAsync();
  47. if (res != null)
  48. {
  49. this.txtIp.Text = res.ServerIp;
  50. this.txtPort.Text = res.Port.ToString();
  51. this.txtHttpPort.Text=res.HttpPort.ToString();
  52. }
  53. }
  54. }
  55. catch (Exception ex)
  56. {
  57. Serilog.Log.Error(ex,"加载配置信息异常");
  58. DxHelper.MsgBoxHelper.ShowError("加载配置信息异常");
  59. }
  60. }
  61. private async void btnSave_Click(object sender, EventArgs e)
  62. {
  63. try
  64. {
  65. using (RHDWContext db = new RHDWContext())
  66. {
  67. var res = await db.SysSetings.FirstOrDefaultAsync();
  68. if (res == null)
  69. {
  70. res = new Model.SysSetings()
  71. {
  72. ServerIp = txtIp.Text,
  73. Port = Convert.ToInt32(txtPort.Text),
  74. HttpPort=Convert.ToInt32(txtHttpPort.Text)
  75. };
  76. db.SysSetings.Add(res);
  77. }
  78. else
  79. {
  80. res.ServerIp = txtIp.Text;
  81. res.Port = Convert.ToInt32(txtPort.Text);
  82. }
  83. await db.SaveChangesAsync();
  84. DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!");
  85. }
  86. }
  87. catch (Exception ex)
  88. {
  89. Serilog.Log.Error(ex, "配置信息保存异常");
  90. DxHelper.MsgBoxHelper.ShowError("配置信息保存异常");
  91. }
  92. }
  93. }
  94. }