CtrlSysSettings.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. using System.IO;
  17. namespace XdCxRhDW.App.UserControl
  18. {
  19. public partial class CtrlSysSettings : DevExpress.XtraEditors.XtraUserControl
  20. {
  21. public CtrlSysSettings()
  22. {
  23. InitializeComponent();
  24. }
  25. private async void CtrlSysSettings_Load(object sender, EventArgs e)
  26. {
  27. try
  28. {
  29. txtIp.Properties.Items.Add("127.0.0.1");
  30. string name = Dns.GetHostName();
  31. IPAddress[] ipadrlist = Dns.GetHostAddresses(name);
  32. foreach (IPAddress ipa in ipadrlist)
  33. {
  34. if (ipa.AddressFamily == AddressFamily.InterNetwork)
  35. txtIp.Properties.Items.Add(ipa.ToString());
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. Serilog.Log.Error(ex, "获取本机IPv4地址出错");
  41. }
  42. try
  43. {
  44. txtIp.SelectedIndex = 0;
  45. using (RHDWContext db = new RHDWContext())
  46. {
  47. var res = await db.SysSetings.FirstOrDefaultAsync();
  48. if (res != null)
  49. {
  50. this.txtIp.Text = res.ServerIp;
  51. this.txtPort.Text = res.Port.ToString();
  52. this.txtHttpPort.Text = res.HttpPort.ToString();
  53. if (!string.IsNullOrEmpty(res.XLDirectory))
  54. this.txtXLDirectory.Text = res.XLDirectory.ToString();
  55. if (!string.IsNullOrEmpty(res.MapService))
  56. this.txtMapService.Text = res.MapService.ToString();
  57. }
  58. }
  59. }
  60. catch (Exception ex)
  61. {
  62. Serilog.Log.Error(ex, "加载配置信息异常");
  63. DxHelper.MsgBoxHelper.ShowError("加载配置信息异常");
  64. }
  65. }
  66. private async void btnSave_Click(object sender, EventArgs e)
  67. {
  68. try
  69. {
  70. using (RHDWContext db = new RHDWContext())
  71. {
  72. var res = await db.SysSetings.FirstOrDefaultAsync();
  73. if (res == null)
  74. {
  75. res = new Model.SysSetings()
  76. {
  77. ServerIp = txtIp.Text,
  78. Port = Convert.ToInt32(txtPort.Text),
  79. HttpPort = Convert.ToInt32(txtHttpPort.Text),
  80. XLDirectory = txtXLDirectory.Text,
  81. MapService=txtMapService.Text,
  82. };
  83. db.SysSetings.Add(res);
  84. }
  85. else
  86. {
  87. res.ServerIp = txtIp.Text;
  88. res.Port = Convert.ToInt32(txtPort.Text);
  89. res.XLDirectory = txtXLDirectory.Text;
  90. res.MapService= txtMapService.Text;
  91. }
  92. await db.SaveChangesAsync();
  93. DxHelper.MsgBoxHelper.ShowInfo("配置信息保存成功!");
  94. }
  95. }
  96. catch (Exception ex)
  97. {
  98. Serilog.Log.Error(ex, "配置信息保存异常");
  99. DxHelper.MsgBoxHelper.ShowError("配置信息保存异常");
  100. }
  101. }
  102. }
  103. }