当前位置:   article > 正文

C# 读取config文件_c#读取dllconfig

c#读取dllconfig
        static void Main(string[] args)

        {           

//读取当前exe的config:App.config

//读取appSettings节点里的数据

            NameValueCollection nameValues = ConfigurationManager.AppSettings;
            string ssssss = nameValues["Key"];

//读取当前exe的config:App.config

        //使用exe路径

            Configuration configuration = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location);
            AppSettingsSection ass = configuration.AppSettings;
            string sss = ass.Settings["Key"].Value;

       //读取configSections里的数据
            ConfigurationSectionGroup group = configuration.GetSectionGroup("Group");

            AppSettingsSection group1 = (AppSettingsSection)group.Sections.Get("Group1");


            string str = group1.Settings["Key"].Value;

            Console.WriteLine(str);

//改变值

            group1.Settings["Key"].Value = "ChangedValue";

// 保存

            configuration.Save();

//读取dll的config文件:TestDll.dll.config

            Type tp = typeof(Class1);
            Configuration config = ConfigurationManager.OpenExeConfiguration(tp.Assembly.Location);
            ConfigurationSectionGroup _group = config.GetSectionGroup("Group");
            AppSettingsSection appss = (AppSettingsSection)_group.Sections.Get("SubGroup");
            Console.WriteLine(appss.Settings["TestKey"].Value);

            Console.ReadLine();

        }

App.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 
  <configSections>
    <sectionGroup name="Group">
      <section name="Group1" type="System.Configuration.AppSettingsSection"/>
      <section name="Group2" type="System.Configuration.AppSettingsSection"/>
    </sectionGroup>
  </configSections>
  <Group>
    <Group1>
      <add key ="Key" value="10"/>
    </Group1>
    <Group2>
      <add key ="Key" value="10"/>
    </Group2>
  </Group>
 
  <appSettings>
    <add key="Key" value="Value"/>
  </appSettings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
</configuration>

TestDll.dll.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="Group">
      <section name="SubGroup" type="System.Configuration.AppSettingsSection"/>
    </sectionGroup>  
  </configSections>
  <Group>
    <SubGroup>
      <add key="TestKey" value="TestValue"/>
    </SubGroup>
  </Group>
</configuration>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/239405
推荐阅读
相关标签
  

闽ICP备14008679号