C# wpf Config配置的读取的两种方式

        1.通过ConfigurationManager来进行读取Config文件中的AppSettings节点

        

public struct AppSetting
        {
                public bool IsOK { get; set; } 
                public string Value { get; set; }    
         }

        /// <summary>
        /// 获取ConfigurationManager.AppSettings中的值
        /// </summary>
        /// <param name="name">keyName</param>
        /// <returns></returns>
        public static AppSetting TryGetAppSetting(string name)
        {
            AppSetting appSetting = new AppSetting();
            if (ConfigurationManager.AppSettings.AllKeys.Contains(name))
            {
                appSetting.Value = ConfigurationManager.AppSettings[name];
                appSetting.IsOK = true;
            }
            else
            {
                appSetting.IsOK = false;
            }
            return appSetting;
        }
        /// <summary>
        /// 增加或者修改appSettings
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void TrySetAppSetting(string name, string value)
        {
            ConfigurationManager.AppSettings [name] = value;
            ExeConfigurationFileMap map = new ExeConfigurationFileMap();
            Assembly assembly = Assembly.GetCallingAssembly();
            Uri uri = new Uri(Path.GetDirectoryName(assembly.CodeBase));
            map.ExeConfigFilename = Path.Combine(uri.LocalPath, assembly.GetName().Name + ".exe.config");
            System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, 0);
            if (config.AppSettings.Settings.AllKeys.Contains(name))
            {
                config.AppSettings.Settings[name].Value = value;
            }
            else
            {
                config.AppSettings.Settings.Add(name,value) ;
            }
            config.Save(System.Configuration.ConfigurationSaveMode.Modified);
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
        }

2.使用ContainerBuilder 读取固定配置

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Configuration.Json;
 /// <summary>
        /// 配置服务
        /// </summary>
        /// <param name="containerBuilder"></param>
        private static void ConfigureServices(ContainerBuilder containerBuilder)
        {
            containerBuilder.RegisterType<MainWindow>();
            containerBuilder.RegisterInstance(ILogger);
            containerBuilder.RegisterType<CancellationTokenSource>();
            Configuration = new ConfigurationBuilder().SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
           .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true })
           .Build();
            containerBuilder.RegisterInstance(Configuration.GetSection("AppSettings").Get<AppSettings>());
            containerBuilder.RegisterInstance(Configuration.GetSection("ScanSettings").Get<ScanSettings>());
            Services = containerBuilder.Build();
            App.ILogger.Info("配置和注册");
        }



    public class AppSettings
    {
        public double 属性{ get; set; }

        public double 属性{ get; set; }

        public double 属性{ get; set; }

        public double 属性{ get; set; }

        public double 属性{ get; set; }
    }
    public class ScanSettings
    {
        public double 属性{ get; set; }

         public double 属性{ get; set; }

         public double 属性{ get; set; }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值