Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <configSections>
- <!--
- name = Имя, которое используется для ссылки на данный раздел в файле настройки.
- type = Обработчик раздела настроек. Включает две секции: полный путь - пространство имен обработчика наших данных + имя самого обработчика, наименование сборки, где данный класс располагается.
- -->
- <section name="StartupFolders" type="ConfigurationSectionTests.StartupFoldersConfigSection, ConfigurationSectionTests"/>
- </configSections>
- <StartupFolders>
- <BasePath path="C:\" />
- <Folders>
- <folder folderType="A" path="c:\foo" />
- <folder folderType="B" path="C:\foo1" />
- </Folders>
- </StartupFolders>
- </configuration>
- */
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Text;
- namespace ConfigurationSectionTests
- {
- public class StartupFoldersConfigSection : ConfigurationSection
- {
- [ConfigurationProperty("Folders")]
- public FoldersCollection FolderItems
- {
- get { return (FoldersCollection)(base["Folders"]); }
- }
- [ConfigurationProperty("BasePath")]
- public BasePath BasePath
- {
- get { return (BasePath) base["BasePath"]; }
- }
- }
- // Если не добавлять AddItemName, то по умолчанию идет add.
- [ConfigurationCollection(typeof(FolderElement), AddItemName = "folder")]
- public class FoldersCollection : ConfigurationElementCollection
- {
- protected override ConfigurationElement CreateNewElement()
- {
- return new FolderElement();
- }
- protected override object GetElementKey(ConfigurationElement element)
- {
- return ((FolderElement)(element)).FolderType;
- }
- public FolderElement this[int idx]
- {
- get { return (FolderElement)BaseGet(idx); }
- }
- }
- public class FolderElement : ConfigurationElement
- {
- [ConfigurationProperty("folderType", DefaultValue = "", IsKey = true, IsRequired = true)]
- public string FolderType
- {
- get { return ((string)(base["folderType"])); }
- set { base["folderType"] = value; }
- }
- [ConfigurationProperty("path", DefaultValue = "", IsKey = false, IsRequired = false)]
- public string Path
- {
- get { return ((string)(base["path"])); }
- set { base["path"] = value; }
- }
- }
- public class BasePath : ConfigurationElement
- {
- [ConfigurationProperty("path", DefaultValue = "", IsKey = true, IsRequired = true)]
- public string Path
- {
- get { return ((string)(base["path"])); }
- set { base["path"] = value; }
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- var section = (StartupFoldersConfigSection)ConfigurationManager.GetSection("StartupFolders");
- if (section != null)
- {
- System.Diagnostics.Debug.WriteLine(section.FolderItems[0].FolderType);
- System.Diagnostics.Debug.WriteLine(section.FolderItems[0].Path);
- }
- var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
- var editSection = (StartupFoldersConfigSection)cfg.Sections["StartupFolders"];
- if (editSection != null)
- {
- System.Diagnostics.Debug.WriteLine(editSection.FolderItems[0].FolderType);
- System.Diagnostics.Debug.WriteLine(editSection.FolderItems[0].Path);
- editSection.FolderItems[0].Path = "C:\\Nanook";
- cfg.Save(); //устанавливает перенос на новую строку и производит проверку <exename>.vshost.exe.config файла в вашей отладочной папке.
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment