Honoo.Configuration.ConfigurationManager 1.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Honoo.Configuration.ConfigurationManager --version 1.1.1                
NuGet\Install-Package Honoo.Configuration.ConfigurationManager -Version 1.1.1                
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.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Honoo.Configuration.ConfigurationManager --version 1.1.1                
#r "nuget: Honoo.Configuration.ConfigurationManager, 1.1.1"                
#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.1.1

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

Honoo.Configuration.ConfigurationManager

简介

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

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

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

快速上手

NuGet

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

引用命名空间


using Honoo.Configuration;

appSettings


public static void Create()
{
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    string filePath = Assembly.GetEntryAssembly().Location + ".config";
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        //
        // 直接赋值等同于 AddOrUpdate 方法。
        //
        manager.AppSettings.Properties.AddOrUpdate("prop1", Common.Random.NextDouble().ToString());
        manager.AppSettings.Properties["prop2"] = Common.Random.NextDouble().ToString();
        manager.AppSettings.Properties["prop3"] = "等待移除";
        //
        // 移除属性的方法。选择其一。
        //
        manager.AppSettings.Properties.Remove("prop3");
        manager.AppSettings.Properties["prop3"] = null;
        manager.AppSettings.Properties.AddOrUpdate("prop3", null);
        //
        // 保存到创建实例时指定的文件。
        //
        manager.Save();
    }
}

public static string Load()
{
    StringBuilder result = new StringBuilder();
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    string filePath = Assembly.GetEntryAssembly().Location + ".config";
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        //
        // 取出属性。
        //
        if (manager.AppSettings.Properties.TryGetValue("prop1", out string value))
        {
            result.AppendLine(value);
        }
        value = manager.AppSettings.Properties["prop2"];
        result.AppendLine(value);
    }
    return result.ToString();
}

connectionStrings


public static void Create()
{
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    string filePath = Assembly.GetEntryAssembly().Location + ".config";
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        SqlConnectionStringBuilder builder1 = new SqlConnectionStringBuilder()
        {
            DataSource = "127.0.0.1",
            InitialCatalog = "DemoCatalog",
            UserID = "sa",
            Password = Common.Random.Next(1000000, 9999999).ToString()
        };
        SqlConnection conn1 = new SqlConnection(builder1.ConnectionString);
        MySqlConnectionStringBuilder builder2 = new MySqlConnectionStringBuilder()
        {
            Server = "127.0.0.1",
            Database = "DemoDB",
            UserID = "root",
            Password = Common.Random.Next(1000000, 9999999).ToString()
        };
        MySqlConnection conn2 = new MySqlConnection(builder2.ConnectionString);
        //
        // 直接赋值等同于 AddOrUpdate 方法。
        //
        manager.ConnectionStrings.Properties.AddOrUpdate("prop1", conn1);
        manager.ConnectionStrings.Properties["prop2"] = new ConnectionStringsValue(conn1);
        manager.ConnectionStrings.Properties.AddOrUpdate("prop3", conn2.ConnectionString, typeof(MySqlConnection).Namespace);
        manager.ConnectionStrings.Properties.AddOrUpdate("prop4", conn2.ConnectionString, typeof(MySqlConnection).AssemblyQualifiedName);
        //
        // 不设置引擎参数,读取时不能直接创建连接实例。
        //
        manager.ConnectionStrings.Properties["prop5"] = new ConnectionStringsValue(conn2.ConnectionString, string.Empty);
        //
        // 移除属性的方法。选择其一。
        //
        manager.ConnectionStrings.Properties.Remove("prop5");
        manager.ConnectionStrings.Properties["prop5"] = null;
        manager.ConnectionStrings.Properties.AddOrUpdate("prop5", (DbConnection)null);
        //
        // 保存到创建实例时指定的文件。
        //
        manager.Save();
    }
}

public static string Load()
{
    StringBuilder result = new StringBuilder();
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    string filePath = Assembly.GetEntryAssembly().Location + ".config";
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        //
        // 取出属性。
        //
        if (manager.ConnectionStrings.Properties.TryGetValue("prop1", out ConnectionStringsValue value))
        {
            result.AppendLine(value.Connection.ConnectionString);
        }
        //
        // 不访问 Connection,属性内部没有实例化 Connection。项目没有引用相关数据库引擎时使用。
        //
        string connectionString = manager.ConnectionStrings.Properties["prop2"].ConnectionString;
        result.AppendLine(connectionString);
        DbConnection connection = manager.ConnectionStrings.Properties["prop3"].Connection;
        result.AppendLine(connection.ConnectionString);

        MySqlConnection mysql = (MySqlConnection)manager.ConnectionStrings.Properties["prop4"].Connection;
        result.AppendLine(mysql.ConnectionString);
    }
    return result.ToString();
}

sectionGroup/section


public static void Create()
{
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    string filePath = Assembly.GetEntryAssembly().Location + ".config";
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        //
        // 支持三种标准类型的创建。
        // System.Configuration.DictionarySectionHandler
        // System.Configuration.NameValueSectionHandler
        // System.Configuration.SingleTagSectionHandler
        //
        // 直接赋值等同于 AddOrUpdate 方法。
        //
        SingleTagSection section1 = (SingleTagSection)manager.ConfigSections.Sections.GetOrAdd("section1", ConfigSectionType.SingleTagSection);
        section1.Properties.AddOrUpdate("prop1", Common.Random.NextDouble().ToString());
        section1.Properties["prop2"] = Common.Random.NextDouble().ToString();
        NameValueSection section2 = (NameValueSection)manager.ConfigSections.Sections.GetOrAdd("section2", ConfigSectionType.NameValueSection);
        section2.Properties.AddOrUpdate("prop1", Common.Random.NextDouble().ToString());
        section2.Properties["prop2"] = Common.Random.NextDouble().ToString();
        //
        ConfigSectionGroup group = manager.ConfigSections.Groups.GetOrAdd("sectionGroup1");
        DictionarySection section3 = (DictionarySection)group.Sections.GetOrAdd("section3", ConfigSectionType.DictionarySection);
        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; // 避免浮点数溢出 Net40 bug
        section3.Properties["prop11"] = double.MaxValue / 2; // 避免浮点数溢出 Net40 bug
        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 种单值类型";
        //
        // 支持自定义类型的创建。
        //
        CustumSection section4 = (CustumSection)manager.ConfigSections.Sections.GetOrAdd("section4", ConfigSectionType.CustumSection);
        section4.SetValue("<arbitrarily>任意文本内容或 XML 内容</arbitrarily><arbitrarily>任意文本内容或 XML 内容</arbitrarily>");
        //
        // 保存到创建实例时指定的文件。
        //
        manager.Save();
    }
}

public static string Load()
{
    StringBuilder result = new StringBuilder();
    //
    // 使用 .NET 程序的默认配置文件或自定义配置文件。
    //
    string filePath = Assembly.GetEntryAssembly().Location + ".config";
    using (ConfigurationManager manager = new ConfigurationManager(filePath))
    {
        //
        // 取出属性。
        //
        if (manager.ConfigSections.Sections.TryGetValue("section1", out SingleTagSection section1))
        {
            foreach (KeyValuePair<string, string> prop in section1.Properties)
            {
                result.AppendLine(prop.Value);
            }
        }
        if (manager.ConfigSections.Sections.TryGetValue("section2", out NameValueSection section2))
        {
            foreach (KeyValuePair<string, string> prop in section2.Properties)
            {
                result.AppendLine(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)
                {
                    result.AppendLine($"{prop.Value.GetType().Name,-10}{prop.Value}");
                }
            }
        }
        //
        // 如果是自定义格式,可取出文本处理。
        //
        if (manager.ConfigSections.Sections.TryGetValue("section4", out CustumSection section4))
        {
            result.AppendLine(section4.Value);
        }
    }
    return result.ToString();
}

自动保存

如果在创建 Honoo.Configuration.ConfigurationManager 实例时没有指定文件路径,此选项无效。默认值是 false。

manager.AutoSave = true;

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

版权

Honoo.Configuration.ConfigurationManager 的开发和发布基于 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 95 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,758 6/16/2022
1.0.9 427 5/20/2022