Search code examples
c#assemblyreflectionsettings

How to access (EntryAssemblyName).Properties.Settings in C#


Here is the shared code in many projects by linking:

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 is shared to many projects by simple file linking like below:

Project Perpose
CommonLibs Test
Project1 Using
Project2 Using
Project3 Using
...

and this is Settings for the projects :

Name Tupe Scope Value
...
LastOpenPath string user "C:\"
LastFilterIndex int user 1
...

I just want to access the shared Properties in the Settings.Default in each project.

I found how to access the Types in the EntryAssembly.(Runtime Assembly?)

But I couldn't find how to get the 'Default' and their members.

How can I do that?


Solution

  • I solved it myself.

    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;
        }
    }
    

    It runs, but it's not smart neat. ok.