Revit二次开发之读取配置文件App.config内sectionName的值(三)

版本:

 

    VS2015

    Revit2018

实现功能:

    直接获取指定sectionGroup下sectionName的值,并实现修改;

    对C盘下生成的.config文件进行修改(生成程序产生的.config文件并没有做出修改)。

 

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="Group">
      <section name="CO" type="System.Configuration.AppSettingsSection"/>
    </sectionGroup>
  </configSections>
  <Group>
    <CO>
      <add key="ABC" value="abcd" />
      <add key="DEF" value="False" />
      <add key="GHI" value="1" />
    </CO>
  </Group>
</configuration>

关键代码

            #region 测试4:直接获取指定sectionGroup下sectionName的值
            string groupName = "Group";
            string sectionName = "CO";
            string key = "ABC";


            //获取Configuration对象
            string assemblyPath = this.GetType().Assembly.Location;
            MessageBox.Show(assemblyPath, "1");


            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            config = ConfigurationManager.OpenExeConfiguration(assemblyPath);


            //访问指定section
            string BBB = String.Format("{0}{1}{2}", groupName, "/", sectionName);
            ConfigurationSection AAA = config.GetSection(BBB);


            if (AAA.SectionInformation.Name == sectionName)
            {
                MessageBox.Show("OK", "4");//成功


                AppSettingsSection section = AAA as AppSettingsSection;


                //添加
                section.Settings.Add("cheng", "aaaaa");
                //修改
                section.Settings["cheng"].Value = "成功了!";
                删除
                //section.Settings.Remove("cheng");


                MessageBox.Show("666", "5");//成功
                foreach (KeyValueConfigurationElement keyvalue in section.Settings)
                {
                    if (keyvalue.Key.ToString() == key)
                    {


                        MessageBox.Show(keyvalue.Key + ":" + keyvalue.Value, "7");
                    }


                }
            }
            config.Save();
            #endregion

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值