CtrlSysSettings.cs 3.9 KB

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