Honoo.Configuration.ConfigurationManager 1.2.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Honoo.Configuration.ConfigurationManager --version 1.2.3                
NuGet\Install-Package Honoo.Configuration.ConfigurationManager -Version 1.2.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Honoo.Configuration.ConfigurationManager" Version="1.2.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Honoo.Configuration.ConfigurationManager --version 1.2.3                
#r "nuget: Honoo.Configuration.ConfigurationManager, 1.2.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Honoo.Configuration.ConfigurationManager as a Cake Addin
#addin nuget:?package=Honoo.Configuration.ConfigurationManager&version=1.2.3

// Install Honoo.Configuration.ConfigurationManager as a Cake Tool
#tool nuget:?package=Honoo.Configuration.ConfigurationManager&version=1.2.3                

Honoo.Configuration.ConfigurationManager

INTRODUCTION

此项目是 System.Configuration.ConfigurationManager 的简单替代。

用于 .NET Framework 4.0+/.NET Standard 2.0+ 中读写默认配置文件或自定义配置文件。

提供对 appSettings、connectionStrings、configSections 节点的有限读写支持。

CHANGELOG

1.2.3

*Feature 移除了访问 ConnectionStrings 直接创建和访问实例的代码。提供 CreateInstance() 方法主动调用。

1.2.2

*Fix 实例释放后仍可访问缓存后的节点的问题。

*Feature ConfigSectionType 更名 ConfigSectionKind。

1.2.1

*Fix 创建 TextSection 时遗漏了 type 属性。

*Fix 读取 DictionarySection 时遗漏了 type 属性。

1.2.0

*Feature 移除了原有的自动保存的代码,现在在事件中实现自动保存。

*Feature 现在支持读写注释(comment)节点。

USAGE

NuGet

https://www.nuget.org/packages/Honoo.Configuration.ConfigurationManager/

Namespace


using Honoo.Configuration;

appSettings


internal static void Create(string filePath)
{
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    bool exists = File.Exists(filePath);
    using (ConfigurationManager manager = exists ? new ConfigurationManager(filePath) : new ConfigurationManager())
    {
        //
        // 直接赋值等同于 AddOrUpdate 方法。
        //
        manager.AppSettings.Properties["prop1"] = Common.Random.NextDouble().ToString();
        manager.AppSettings.Properties.AddOrUpdate("prop2", Common.Random.NextDouble().ToString());
        manager.AppSettings.Properties.AddOrUpdate("prop3", Common.Random.NextDouble().ToString());
        //
        // 设置注释。
        //
        manager.AppSettings.Properties.TrySetComment("prop1", string.Empty);
        manager.AppSettings.Properties.TrySetComment("prop2", "This is \"appSettings\" prop2 comment");
        manager.AppSettings.Properties.TrySetComment("prop3", null);
        //
        // 移除属性的方法。选择其一。移除属性时相关注释一并移除。
        //
        manager.AppSettings.Properties.Remove("prop1");
        manager.AppSettings.Properties["prop1"] = null;
        manager.AppSettings.Properties.AddOrUpdate("prop1", null);
        //
        // 保存到指定的文件。
        //
        manager.Save(filePath);
    }
}

internal static void Load(string filePath)
{
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        //
        // 取出属性。
        //
        if (manager.AppSettings.Properties.TryGetValue("prop2", out string value))
        {
            Console.WriteLine(value);
        }
        value = manager.AppSettings.Properties["prop3"];
        Console.WriteLine(value);
        //
        // 取出注释。
        //
        if (manager.AppSettings.Properties.TryGetComment("prop2", out string comment))
        {
            Console.WriteLine(comment);
        }
    }
}

connectionStrings


internal static void Create(string filePath)
{
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        SqlConnectionStringBuilder builder1 = new SqlConnectionStringBuilder()
        {
            DataSource = "127.0.0.1",
            InitialCatalog = "DemoCatalog",
            UserID = "sa",
            Password = "12345"
        };
        SqlConnection conn1 = new SqlConnection(builder1.ConnectionString);
        MySqlConnectionStringBuilder builder2 = new MySqlConnectionStringBuilder()
        {
            Server = "127.0.0.1",
            Database = "DemoDB",
            UserID = "root",
            Password = "12345"
        };
        MySqlConnection conn2 = new MySqlConnection(builder2.ConnectionString);
        //
        // 直接赋值等同于 AddOrUpdate 方法。不设置引擎参数,读取时不能访问连接实例。
        //
        manager.ConnectionStrings.Properties["prop1"] = new ConnectionStringsValue(conn1.ConnectionString, null);
        manager.ConnectionStrings.Properties["prop2"] = new ConnectionStringsValue(conn1);
        manager.ConnectionStrings.Properties.AddOrUpdate("prop3", conn1);
        manager.ConnectionStrings.Properties.AddOrUpdate("prop4", conn2.ConnectionString, conn2.GetType().Namespace);
        manager.ConnectionStrings.Properties.AddOrUpdate("prop5", conn2.ConnectionString, typeof(MySqlConnection).AssemblyQualifiedName);
        //
        // 设置注释。
        //
        manager.ConnectionStrings.Properties.TrySetComment("prop1", string.Empty);
        manager.ConnectionStrings.Properties.TrySetComment("prop2", "This is \"sql server connection\" comment");
        manager.ConnectionStrings.Properties.TrySetComment("prop3", null);
        manager.ConnectionStrings.Properties.TrySetComment("prop4", "This is \"mysql connection\" comment ");
        manager.ConnectionStrings.Properties.TrySetComment("prop5", "This is \"mysql connection\" comment used assembly qualified name");
        //
        // 移除属性的方法。选择其一。移除属性时相关注释一并移除。
        //
        manager.ConnectionStrings.Properties.Remove("prop1");
        manager.ConnectionStrings.Properties["prop1"] = null;
        manager.ConnectionStrings.Properties.AddOrUpdate("prop1", (DbConnection)null);
        //
        // 保存到指定的文件。
        //
        manager.Save(filePath);
    }
}

internal static void Load(string filePath)
{
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        //
        // 取出属性。
        //
        if (manager.ConnectionStrings.Properties.TryGetValue("prop2", out ConnectionStringsValue value))
        {
            Console.WriteLine(value.ConnectionString);
        }
        string connectionString = manager.ConnectionStrings.Properties["prop3"].ConnectionString;
        Console.WriteLine(connectionString);
        //
        // 访问实例。
        //
        DbConnection connection = manager.ConnectionStrings.Properties["prop4"].CreateInstance();
        Console.WriteLine(connection.ConnectionString);

        MySqlConnection mysql = (MySqlConnection)manager.ConnectionStrings.Properties["prop5"].CreateInstance();
        Console.WriteLine(mysql.ConnectionString);
    }
}

sectionGroup/section


internal static void Create(string filePath)
{
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        //
        // 支持三种标准类型的创建。
        // System.Configuration.DictionarySectionHandler
        // System.Configuration.NameValueSectionHandler
        // System.Configuration.SingleTagSectionHandler
        //
        // 直接赋值等同于 AddOrUpdate 方法。
        //
        SingleTagSection section1 = (SingleTagSection)manager.ConfigSections.Sections.GetOrAdd("section1", ConfigSectionKind.SingleTagSection);
        section1.Properties.AddOrUpdate("prop1", Common.Random.NextDouble().ToString());
        section1.Properties["prop2"] = Common.Random.NextDouble().ToString();
        NameValueSection section2 = (NameValueSection)manager.ConfigSections.Sections.GetOrAdd("section2", ConfigSectionKind.NameValueSection);
        section2.Properties.AddOrUpdate("prop1", Common.Random.NextDouble().ToString());
        section2.Properties["prop2"] = Common.Random.NextDouble().ToString();
        section2.Properties.TrySetComment("prop1", "This is a name value section child");
        section2.Properties.TrySetComment("prop2", "This is a name value section child");
        //
        // 配置组和注释。
        //
        ConfigSectionGroup group = manager.ConfigSections.Groups.GetOrAdd("sectionGroup1");
        manager.ConfigSections.Groups.TrySetComment("sectionGroup1", "This is a section group");
        //
        // 配置容器和注释。
        //
        DictionarySection section3 = (DictionarySection)group.Sections.GetOrAdd("section3", ConfigSectionKind.DictionarySection);
        group.Sections.TrySetComment("section3", "This is a dictionary section");
        //
        section3.Properties.AddOrUpdate("prop1", true);
        section3.Properties.AddOrUpdate("prop2", sbyte.MaxValue);
        section3.Properties.AddOrUpdate("prop3", byte.MaxValue);
        section3.Properties.AddOrUpdate("prop4", short.MaxValue);
        section3.Properties.AddOrUpdate("prop5", ushort.MaxValue);
        section3.Properties.AddOrUpdate("prop6", int.MaxValue);
        section3.Properties.AddOrUpdate("prop7", uint.MaxValue);
        section3.Properties["prop8"] = long.MaxValue;
        section3.Properties["prop9"] = ulong.MaxValue;
        section3.Properties["prop10"] = float.MaxValue / 2;
        section3.Properties["prop11"] = double.MaxValue / 2;
        section3.Properties["prop12"] = decimal.MaxValue;
        section3.Properties["prop13"] = (char)Common.Random.Next(65, 91);
        section3.Properties["prop14"] = new byte[] { 0x01, 0x02, 0x03, 0x0A, 0x0B, 0x0C };
        section3.Properties["prop15"] = "支持 15 种可序列化类型";
        section3.Properties.TrySetComment("prop15", "This is a dictionary section child");
        //
        // 以文本方式创建。
        //
        TextSection section4 = (TextSection)manager.ConfigSections.Sections.GetOrAdd("section4", ConfigSectionKind.TextSection);
        section4.SetAttribute("attr1", "属性值");
        section4.SetValue("<arbitrarily>任意内容</arbitrarily>");
        manager.ConfigSections.Sections.TrySetComment("section4", "This is a text section");
        //
        // 保存到指定的文件。
        //
        manager.Save(filePath);
    }
}

internal static void Load(string filePath)
{
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        //
        // 取出属性。
        //
        if (manager.ConfigSections.Sections.TryGetValue("section1", out SingleTagSection section1))
        {
            foreach (KeyValuePair<string, string> prop in section1.Properties)
            {
                Console.WriteLine(prop.Value);
            }
        }
        if (manager.ConfigSections.Sections.TryGetValue("section2", out NameValueSection section2))
        {
            foreach (KeyValuePair<string, string> prop in section2.Properties)
            {
                Console.WriteLine(prop.Value);
            }
        }
        if (manager.ConfigSections.Groups.TryGetValue("sectionGroup1", out ConfigSectionGroup group))
        {
            if (group.Sections.TryGetValue("section3", out DictionarySection section3))
            {
                // 根据 type 参数返回强类型值。如果没有 type 参数,以 string 类型处理。
                foreach (KeyValuePair<string, object> prop in section3.Properties)
                {
                    Console.WriteLine($"{prop.Value.GetType().Name,-10}{prop.Value}");
                }
            }
        }
        if (manager.ConfigSections.Sections.TryGetValue("section4", out TextSection section4))
        {
            Console.WriteLine(section4.GetXmlString());
        }
    }
}

Auto save

在事件中实现自动保存。


internal static void AutoSaveDemo()
{
    using (ConfigurationManager manager = new ConfigurationManager())
    {
        manager.OnChanged += Manager_OnChanged;
        manager.OnDisposing += Manager_OnDisposing;
    }
}

private static void Manager_OnChanged(ConfigurationManager manager)
{
    manager.Save(filePath);
}

private static void Manager_OnDisposing(ConfigurationManager manager)
{
    manager.Save(filePath);
}

UWP

必须使用流方式。


public static async void Test()
{
    using (var read = await storageFile.OpenStreamForReadAsync())
    {
        using (ConfigurationManager manager = new ConfigurationManager(read))
        {
            using (var write = await storageFile.OpenStreamForWriteAsync())
            {
                manager.Save(write);
            }
        }
    }
}

LICENSE

MIT 协议。

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETFramework 4.0

    • No dependencies.
  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.5.4 92 10/26/2024
1.4.19-final 60 10/21/2024
1.3.4 100 7/1/2024
1.3.2 189 9/17/2023
1.3.1 176 7/18/2023
1.2.5 295 3/3/2023
1.2.3 274 2/22/2023
1.1.1 340 2/4/2023
1.1.0 3,736 6/16/2022
1.0.9 427 5/20/2022