Revit二次開發之簡單的讀取配置文件App.config內的值(二)

版本:

VS2015

Revit2018

實現功能:

對C盤下生成的.config文件進行修改(生成程序產生的.config文件並沒有做出修改)

 

App.config

     <appSettings>

         <clear />

         <addkey="userName"value="" />

         <addkey="password"value="" />

         <addkey="Department"value="" />

         <addkey="returnValue"value="" />

         <addkey="pwdPattern"value="" />

         <addkey="userPattern"value="" />

</appSettings>

關鍵代碼:

 末尾註釋掉的幾行可以實現操作

            #region 測試3:修改配置文件

            string groupName = "Group";
            string sectionName = "CutOption";
            string key = "CutScope";

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

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

            #region 添加<appSettings>
            //添加
            config.AppSettings.Settings.Add("wyg", "1");

            //修改
            config.AppSettings.Settings["wyg"].Value = "2";

            config.Save();
            #endregion


            //獲取相應的SectionGroup
            ConfigurationSectionGroup sectiongroup = config.GetSectionGroup(groupName);
            if (sectiongroup.ToString() == null)
            {
                MessageBox.Show("sectiongroup is null", "2");
            }
            //SectionGroup下的所有Sections(包括所有section及其內容)
            ConfigurationSectionCollection sections = sectiongroup.Sections;
            foreach (ConfigurationSection configsection in sections)
            {
                //獲取指定的sectionName
                if (configsection.SectionInformation.Name == sectionName)
                {
                    MessageBox.Show("OK", "3");
                }

                //sectionName下的各個 pSettings 配置節提供配置系統支持。
                AppSettingsSection section = configsection as AppSettingsSection;
                foreach (KeyValueConfigurationElement keyvalue in section.Settings)
                {
                    if (keyvalue.Key.ToString() == key)
                    {
                        MessageBox.Show(keyvalue.Key + ":" + keyvalue.Value, "4");
                    }
                }
            }
            MessageBox.Show(sectiongroup.ToString(), "sectiongroup is not null");


            ////寫入<add>元素的Value  
            //config.AppSettings.Settings["name"].Value = "xieyc";
            ////增加<add>元素  
            //config.AppSettings.Settings.Add("wyg", "WYG");
            ////刪除<add>元素  
            //config.AppSettings.Settings.Remove("name");
            ////一定要記得保存,寫不帶參數的config.Save()也可以  
            //config.Save(ConfigurationSaveMode.Modified);
            ////刷新,否則程序讀取的還是之前的值(可能已裝入內存)  
            //System.Configuration.ConfigurationManager.RefreshSection("appSettings");



            #endregion

效果:

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章