nyk0r

ConfigurationSection

Sep 16th, 2011
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.19 KB | None | 0 0
  1. /*
  2. <?xml version="1.0" encoding="utf-8" ?>
  3. <configuration>
  4.   <configSections>
  5.     <!--
  6.     name = Имя, которое используется для ссылки на данный раздел в файле настройки.
  7.     type = Обработчик раздела настроек. Включает две секции: полный путь - пространство имен обработчика наших данных + имя самого обработчика, наименование сборки, где данный класс располагается.
  8.     -->
  9.     <section name="StartupFolders" type="ConfigurationSectionTests.StartupFoldersConfigSection, ConfigurationSectionTests"/>
  10.   </configSections>
  11.   <StartupFolders>
  12.     <BasePath path="C:\" />
  13.     <Folders>
  14.       <folder folderType="A" path="c:\foo" />
  15.       <folder folderType="B" path="C:\foo1" />
  16.     </Folders>
  17.   </StartupFolders>
  18. </configuration>
  19. */
  20.  
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Configuration;
  24. using System.Linq;
  25. using System.Text;
  26.  
  27. namespace ConfigurationSectionTests
  28. {
  29.     public class StartupFoldersConfigSection : ConfigurationSection
  30.     {
  31.         [ConfigurationProperty("Folders")]
  32.         public FoldersCollection FolderItems
  33.         {
  34.             get { return (FoldersCollection)(base["Folders"]); }
  35.         }
  36.  
  37.         [ConfigurationProperty("BasePath")]
  38.         public BasePath BasePath
  39.         {
  40.             get { return (BasePath) base["BasePath"]; }
  41.         }
  42.     }
  43.  
  44.     // Если не добавлять AddItemName, то по умолчанию идет add.
  45.     [ConfigurationCollection(typeof(FolderElement), AddItemName = "folder")]
  46.     public class FoldersCollection : ConfigurationElementCollection
  47.     {
  48.         protected override ConfigurationElement CreateNewElement()
  49.         {
  50.             return new FolderElement();
  51.         }
  52.  
  53.         protected override object GetElementKey(ConfigurationElement element)
  54.         {
  55.             return ((FolderElement)(element)).FolderType;
  56.         }
  57.  
  58.         public FolderElement this[int idx]
  59.         {
  60.             get { return (FolderElement)BaseGet(idx); }
  61.         }
  62.     }
  63.  
  64.  
  65.     public class FolderElement : ConfigurationElement
  66.     {
  67.         [ConfigurationProperty("folderType", DefaultValue = "", IsKey = true, IsRequired = true)]
  68.         public string FolderType
  69.         {
  70.             get { return ((string)(base["folderType"])); }
  71.             set { base["folderType"] = value; }
  72.         }
  73.  
  74.         [ConfigurationProperty("path", DefaultValue = "", IsKey = false, IsRequired = false)]
  75.         public string Path
  76.         {
  77.             get { return ((string)(base["path"])); }
  78.             set { base["path"] = value; }
  79.         }
  80.     }
  81.  
  82.     public class BasePath : ConfigurationElement
  83.     {
  84.         [ConfigurationProperty("path", DefaultValue = "", IsKey = true, IsRequired = true)]
  85.         public string Path
  86.         {
  87.             get { return ((string)(base["path"])); }
  88.             set { base["path"] = value; }
  89.         }
  90.     }
  91.  
  92.     class Program
  93.     {
  94.         static void Main(string[] args)
  95.         {
  96.             var section = (StartupFoldersConfigSection)ConfigurationManager.GetSection("StartupFolders");
  97.  
  98.             if (section != null)
  99.             {
  100.                 System.Diagnostics.Debug.WriteLine(section.FolderItems[0].FolderType);
  101.                 System.Diagnostics.Debug.WriteLine(section.FolderItems[0].Path);
  102.             }
  103.  
  104.             var cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
  105.             var editSection = (StartupFoldersConfigSection)cfg.Sections["StartupFolders"];
  106.             if (editSection != null)
  107.             {
  108.                 System.Diagnostics.Debug.WriteLine(editSection.FolderItems[0].FolderType);
  109.                 System.Diagnostics.Debug.WriteLine(editSection.FolderItems[0].Path);
  110.                 editSection.FolderItems[0].Path = "C:\\Nanook";
  111.                 cfg.Save(); //устанавливает перенос на новую строку и производит проверку <exename>.vshost.exe.config файла в вашей отладочной папке.
  112.             }
  113.         }
  114.     }
  115. }
Advertisement
Add Comment
Please, Sign In to add comment