如何在C#中访问(EntryAssemblyName).Properties.Settings

问题描述 投票:0回答:1

这里是很多项目中通过链接共享的代码:

FileUtil.cs

namespace CommonLibs
{
    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();


            // get the entry assembly
            var asm = Assembly.GetEntryAssembly();
            var asmName = asm.GetName().Name;
            var settings = asm.GetType(asmName + ".Properties.Settings");

            // (some code to deal with 'Default' in the settings type : I'm not aware of) 

            // use the 'LastOpenPath' setting for default path 
            InitialDirectory = InitialDirectory ?? settings.Default.LastOpenPath;
            FilterIndex = FilterIndex ?? settings.Default.LastFilterIndex;
            FilterIndex = FilterIndex < 0 ? 1 : FilterIndex;
            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 = "";
            ...
        }
    }
}

FileUtil.cs
通过简单的文件链接共享到许多项目,如下所示:

项目 假设
公共库 测试
项目1 使用
项目2 使用
项目3 使用
...

这是项目的设置:

姓名 图佩 范围 价值
...
最后打开路径 绳子 用户 “C:\”
最后过滤器索引 int 用户 1
...

我只想访问每个项目中Settings.Default中的共享属性。

我找到了如何访问 EntryAssembly 中的类型。(运行时程序集?)

但我找不到如何获取“默认”及其成员。

我怎样才能做到这一点?

c# assembly reflection settings
1个回答
0
投票

我自己解决了。

public static bool OpenFile(out string ResultPath, string FileName, string InitialDirectory = null, string Filter = "すべてのファイル(*.*)|*.*", int? FilterIndex = null, bool CheckFileExists = true, bool CheckPathExists = true)
{
    OpenFileDialog ofd = new OpenFileDialog();


    // get the executing assembly
    var asm = Assembly.GetEntryAssembly();
    var asmName = asm.GetName().Name;
        //GetExecutingAssembly();
    var settings = asm.GetType(asmName + ".Properties.Settings");
    var Default_Type = settings.GetProperty("Default");
    var Default = Default_Type.GetValue(null);
    var LastOpenPath_PropInfo = Default_Type.PropertyType.GetProperty("LastOpenPath");
    var LastFilterIndex_PropInfo = Default_Type.PropertyType.GetProperty("LastFilterIndex");
    var Save_MethodInfo = Default.GetType().GetMethod("Save");

    var LastOpenPath = LastOpenPath_PropInfo.GetValue(Default);
    var LastFilterIndex = LastFilterIndex_PropInfo.GetValue(Default);
    //CommonLibs.Properties.Settings


    // use the 'LastOpenPath' setting for default path 
    InitialDirectory = InitialDirectory ?? LastOpenPath as string;
    FilterIndex = FilterIndex ?? LastFilterIndex as int?;
    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 = "";
    
    //show dialog 
    if (ofd.ShowDialog() == true)
    {
        // return the fullname path only when click 'OK' button
        ResultPath = ofd.FileName;
        FilterIndex = ofd.FilterIndex;
        // reflesh the last path and index infomation

        LastOpenPath_PropInfo.SetValue(Default, ResultPath);
        LastFilterIndex_PropInfo.SetValue(Default, FilterIndex);
        //Settings.Default.LastOpenPath = ResultPath;
        //Settings.Default.LastFilterIndex = FilterIndex.GetValueOrDefault(1);

        Save_MethodInfo.Invoke(Default, null);  
        //Settings.Default.Save();
        return true;
    }
    else
    {
        return false;
    }
}

它可以运行,但并不智能整洁。好的。

© www.soinside.com 2019 - 2024. All rights reserved.