如何在C#中访问入口程序集的Properties.Settings.Default成员
解决方案
要通过反射获取Settings的Default实例及其成员,你需要利用.NET的反射API访问静态属性和实例属性,具体修改如下:
修改后的FileUtil.cs代码
namespace CommonLibs { using System.Reflection; using System.Windows.Forms; // 确保项目引用System.Windows.Forms程序集 internal class FileUtil { public static bool OpenFile(out string ResultPath, string FileName, string InitialDirectory = null, string Filter = "AllFiles(*.*)|*.*", int? FilterIndex = null, bool CheckFileExists = true, bool CheckPathExists = true) { OpenFileDialog ofd = new OpenFileDialog(); // 获取入口程序集及Settings类型 var asm = Assembly.GetEntryAssembly(); var asmName = asm.GetName().Name; var settingsType = asm.GetType($"{asmName}.Properties.Settings"); // 初始化默认配置值 string savedOpenPath = "C:\\"; int savedFilterIndex = 1; if (settingsType != null) { // 获取Settings的静态Default属性,得到实例 var defaultProp = settingsType.GetProperty("Default", BindingFlags.Public | BindingFlags.Static); if (defaultProp != null) { var settingsInstance = defaultProp.GetValue(null); // 读取LastOpenPath配置 var openPathProp = settingsType.GetProperty("LastOpenPath"); if (openPathProp != null) { savedOpenPath = openPathProp.GetValue(settingsInstance) as string ?? savedOpenPath; } // 读取LastFilterIndex配置 var filterIndexProp = settingsType.GetProperty("LastFilterIndex"); if (filterIndexProp != null) { var indexValue = filterIndexProp.GetValue(settingsInstance); savedFilterIndex = indexValue is int val ? val : savedFilterIndex; } } } // 应用配置值或默认值 InitialDirectory = InitialDirectory ?? savedOpenPath; FilterIndex = FilterIndex ?? savedFilterIndex; FilterIndex = FilterIndex < 0 ? 1 : FilterIndex; ofd.FileName = FileName; ofd.InitialDirectory = InitialDirectory; ofd.Filter = Filter; ofd.FilterIndex = FilterIndex.GetValueOrDefault(1); ofd.CheckFileExists = CheckFileExists; ofd.CheckPathExists = CheckPathExists; ResultPath = ""; bool dialogResult = ofd.ShowDialog() == DialogResult.OK; if (dialogResult) { ResultPath = ofd.FileName; // 可选:将本次操作的路径和筛选器索引保存回Settings if (settingsType != null) { var defaultProp = settingsType.GetProperty("Default", BindingFlags.Public | BindingFlags.Static); if (defaultProp != null) { var settingsInstance = defaultProp.GetValue(null); var openPathProp = settingsType.GetProperty("LastOpenPath"); openPathProp?.SetValue(settingsInstance, ofd.InitialDirectory); var filterIndexProp = settingsType.GetProperty("LastFilterIndex"); filterIndexProp?.SetValue(settingsInstance, ofd.FilterIndex); // 调用Save方法持久化配置 var saveMethod = settingsType.GetMethod("Save", BindingFlags.Public | BindingFlags.Instance); saveMethod?.Invoke(settingsInstance, null); } } } return dialogResult; } } }
关键逻辑说明
- 获取
Default实例:通过Type.GetProperty("Default", BindingFlags.Public | BindingFlags.Static)获取Settings类的静态Default属性,再调用GetValue(null)拿到Settings实例(静态属性无需传入实例,所以参数为null)。 - 读取配置成员:通过
Type.GetProperty("属性名")定位目标属性,再调用GetValue(实例)获取属性值,同时添加null校验避免空引用异常。 - 保存配置(可选):如果需要记录用户本次操作的路径和筛选器索引,可通过
SetValue修改属性值,再调用Settings的Save方法完成持久化。
注意事项
- 确保每个引用
FileUtil.cs的项目都已配置LastOpenPath和LastFilterIndex两个用户范围的设置项。 - 代码中添加了多层null校验,避免因程序集结构变化导致运行时异常。
- 项目需引用
System.Windows.Forms程序集(因使用OpenFileDialog)。
内容的提问来源于stack exchange,提问作者KLc3088
相关产品推荐
相关产品推荐

