using System; using System.IO; using System.Xml; using System.Windows.Forms; namespace 成套报价软件 { /// /// 企业信息 的交互逻辑(事件处理)。 /// UI 布局定义见 企业信息.ui.cs。 /// 本窗体负责维护本机成套厂的固定信息(名称/地址/联系人/电话/纳税识别号/法人代表), /// 信息持久化到 shezhi\企业信息.config.xml,供「新建项目」窗口自动调用。 /// public partial class 企业信息 { /// /// 企业信息配置文件相对路径(相对程序根目录)。 /// 与本窗体的 UI 布局 XML(shezhi\企业信息.xml)区分开,此文件是运行时数据。 /// private const string ConfigFile = @"shezhi\企业信息.config.xml"; /// 构造函数:初始化 UI 并绑定事件。 public 企业信息() { InitializeComponent(); this.Load += 企业信息_Load; this.btn确定.Click += btn确定_Click; } /// 窗口加载时,从配置文件回填已保存的企业信息。 private void 企业信息_Load(object sender, EventArgs e) { LoadFromConfig(); } /// “确定”按钮:先把当前编辑的内容持久化,再以 OK 结果关闭窗口。 private void btn确定_Click(object sender, EventArgs e) { SaveToConfig(); } // ============================================================ // 配置文件读写(shezhi\企业信息.config.xml) // ============================================================ /// 配置文件绝对路径(基于程序根目录拼接)。 private static string GetConfigFullPath() { return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, ConfigFile); } /// /// 把当前 6 个文本框内容写入配置文件。 /// 目录不存在会自动创建;失败时弹窗提示,但不抛异常阻断关闭。 /// private void SaveToConfig() { try { string path = GetConfigFullPath(); string dir = Path.GetDirectoryName(path); if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) { Directory.CreateDirectory(dir); } var settings = new XmlWriterSettings { Encoding = System.Text.Encoding.UTF8, Indent = true, }; using (var writer = XmlWriter.Create(path, settings)) { writer.WriteStartElement("企业信息"); writer.WriteElementString("企业名称", txt企业名称.Text ?? ""); writer.WriteElementString("地址", txt地址.Text ?? ""); writer.WriteElementString("联系人", txt联系人.Text ?? ""); writer.WriteElementString("电话", txt电话.Text ?? ""); writer.WriteElementString("纳税识别号", txt纳税识别号.Text ?? ""); writer.WriteElementString("法人代表", txt法人代表.Text ?? ""); writer.WriteEndElement(); } } catch (Exception ex) { MessageBox.Show(this, "保存企业信息失败:\n" + ex.Message, "企业信息", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } /// 从配置文件读取并回填 6 个文本框;文件不存在时静默忽略。 private void LoadFromConfig() { string path = GetConfigFullPath(); if (!File.Exists(path)) return; try { var doc = new XmlDocument(); doc.Load(path); txt企业名称.Text = ReadNodeText(doc, "企业名称"); txt地址.Text = ReadNodeText(doc, "地址"); txt联系人.Text = ReadNodeText(doc, "联系人"); txt电话.Text = ReadNodeText(doc, "电话"); txt纳税识别号.Text = ReadNodeText(doc, "纳税识别号"); txt法人代表.Text = ReadNodeText(doc, "法人代表"); } catch { // 读失败不阻断窗口显示,保持文本框为空 } } /// 安全读取根节点下指定子节点的文本,节点不存在返回空串。 private static string ReadNodeText(XmlDocument doc, string name) { XmlNode node = doc.SelectSingleNode("//" + name); return node != null ? (node.InnerText ?? "") : ""; } // ============================================================ // 公共读取接口(供「新建项目」等窗口调用,无需弹出本窗体) // ============================================================ /// /// 直接从配置文件读取企业信息(不创建窗口)。 /// 文件不存在或读取失败时,各字段返回空串。 /// 键固定为:企业名称/地址/联系人/电话/纳税识别号/法人代表。 /// public static System.Collections.Generic.Dictionary LoadSettings() { var result = new System.Collections.Generic.Dictionary { { "企业名称", "" }, { "地址", "" }, { "联系人", "" }, { "电话", "" }, { "纳税识别号", "" }, { "法人代表", "" }, }; string path = GetConfigFullPath(); if (!File.Exists(path)) return result; try { var doc = new XmlDocument(); doc.Load(path); var keys = new System.Collections.Generic.List(result.Keys); foreach (string key in keys) { XmlNode node = doc.SelectSingleNode("//" + key); if (node != null) { result[key] = node.InnerText ?? ""; } } } catch { // 忽略,返回空值字典 } return result; } } }