2016-07-22 27 views
1

私はこの問題が昨日舐められたと思っていました。ロック時にConfigurationSectionのプロパティを編集できない

  1. config.Save()

を設定へのセクションを追加します。しかし、今日、私はへの呼び出しで再び同じエラーを取得していますセクション

  • に要素を追加し、新しいセクションを作成します。 config.Save()

    元のセクションでは、プロパティを追加したため、問題が発生している可能性があります。インデックスのプロパティ)しかし、私はそれをバックアップしました。

    私の質問はなぜセクションがロックされているのですか?セクションがロックされる原因となる操作ロックを解除するには何をする必要がありますか?セクションを更新するための正しい順序は何ですか?

    構成全体を一度保存​​すればよいですか?私は個々のセクションを変更した後にセーブをしようとしています。

    ここに私のコードのカットです。 ConfigurationSectionおよびConfigurationElementフォームから派生したFolderSectionおよびFolderElementを除きます。 UpdateFolders()への最初の呼び出しは正常に動作します。 2番目の呼び出しは、内部例外 "ConfigurationSectionのプロパティはロックされていると編集できません"を含むconfig.Save()の呼び出しで失敗します。

    using System; 
    using System.Collections; 
    using System.Collections.Generic; 
    using System.Linq; 
    
    using Microsoft.VisualStudio.TestTools.UnitTesting; 
    
    using System.IO; 
    using System.Configuration; 
    
    // using Common.Core; 
    // using Common.Config; 
    
    namespace Common.Test 
    { 
        public class FolderElement : ConfigurationElement 
        { 
         protected const string NameKey = "name"; 
         protected const string VolumeKey = "volume"; 
         protected const string PathKey = "path"; 
         protected const string selectedKey = "selected"; 
         protected const string activeKey = "active"; 
    
        [ConfigurationProperty(NameKey, DefaultValue = "", IsKey = true, IsRequired = true)] 
        public string Name 
        { 
         get { return (string)base[NameKey]; } 
         set { base[NameKey] = value; } 
        } 
    
        [ConfigurationProperty(VolumeKey, DefaultValue = "", IsKey = false, IsRequired = false)] 
        public string VolumeLabel 
        { 
         get { return (string)base[VolumeKey]; } 
         set { base[VolumeKey] = value; } 
        } 
    
        [ConfigurationProperty(PathKey, DefaultValue = "", IsKey = false, IsRequired = true)] 
        public string Path 
        { 
         get { return (string)base[PathKey]; } 
         set { base[PathKey] = value; } 
        } 
    
        [ConfigurationProperty(selectedKey, DefaultValue = "false", IsKey = false, IsRequired = false)] 
        public bool Selected 
        { 
         get { return (bool)base[selectedKey]; } 
         set { base[selectedKey] = value; } 
        } 
    
        [ConfigurationProperty(activeKey, DefaultValue = "true", IsKey = false, IsRequired = false)] 
        public bool Active 
        { 
         get { return (bool)base[activeKey]; } 
         set { base[activeKey] = value; } 
        } 
    } 
    
    [ConfigurationCollection(typeof(FolderElement))] 
    public class FolderCollection : ConfigurationElementCollection 
    { 
        internal const string _elementName = "elements"; 
    
        protected override string ElementName 
        { 
         get { return _elementName; } 
        } 
    
        public override ConfigurationElementCollectionType CollectionType 
        { 
         get { return ConfigurationElementCollectionType.AddRemoveClearMap; } 
        } 
    
        protected override bool IsElementName(string elementName) 
        { 
         return elementName.Equals(_elementName, StringComparison.InvariantCultureIgnoreCase); 
        } 
    
        public override bool IsReadOnly() 
        { 
         return false; 
        } 
    
        protected override ConfigurationElement CreateNewElement() 
        { 
         return new FolderElement(); 
        } 
    
        /// <summary> 
        /// Return key value for element. 
        /// </summary> 
        /// <param name="element"></param> 
        /// <returns></returns> 
        /// <remarks></remarks> 
        protected override object GetElementKey(ConfigurationElement element) 
        { 
         return ((FolderElement)element).Name; 
        } 
    
        /// <summary> 
        /// Default index property. 
        /// </summary> 
        /// <param name="index"></param> 
        /// <returns></returns> 
        public FolderElement this[int index] 
        { 
         get { return (FolderElement)BaseGet(index); } 
        } 
    
    
        /// <summary> 
        /// Returns content element by name. 
        /// </summary> 
        /// <param name="name"></param> 
        /// <returns></returns> 
        public FolderElement GetElementByName(string name) 
        { 
         return (FolderElement)BaseGet(name); 
        } 
    
    
        public IEnumerable<FolderElement> Elements 
        { 
         get 
         { 
          for (int index = 0; index < this.Count; index++) yield return (FolderElement)BaseGet(index); 
         } 
        } 
    
        /// <summary> 
        /// Add an element to the collection 
        /// </summary> 
        /// <param name="element"></param> 
        public void AddElement(FolderElement element) 
        { 
         BaseAdd(element); 
        } 
    
    
    } 
    
    public class FolderSection : ConfigurationSection 
    { 
    
        // Attribute argument must be a constant expression. 
        protected const string _elementsTag = "elements"; 
    
        [ConfigurationProperty(_elementsTag, Options = ConfigurationPropertyOptions.IsDefaultCollection)] 
        public FolderCollection Elements 
        { 
         get { return ((FolderCollection)(base[_elementsTag])); } 
         set { base[_elementsTag] = value; } 
        } 
    
    } 
    
    
    [TestClass] 
    public class TestConfig 
    { 
        private string _appName = "Test"; 
        private string _appFolder = null; 
    
        private static string _exec = Path.GetFileName(Environment.GetCommandLineArgs()[0]); 
    
    
        public string AppFolder 
        { 
         get 
         { 
          return (_appFolder == null) 
           ? _appFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "myProjects", _appName) 
           : _appFolder; 
         } 
        } 
    
    
        public ExeConfigurationFileMap GetFileMap() 
        { 
    
         var fileMap = new ExeConfigurationFileMap(); 
         fileMap.ExeConfigFilename = Path.Combine(AppContext.BaseDirectory, _exec + ".config"); 
         fileMap.RoamingUserConfigFilename = Path.Combine(AppFolder, "App.config"); 
         fileMap.LocalUserConfigFilename = Path.Combine(AppFolder, "App.config"); 
    
         return fileMap; 
    
        } 
    
    
        [TestMethod] 
        public void SaveConfigTest() 
        { 
         var fileMap = GetFileMap(); 
         var userConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.PerUserRoamingAndLocal); 
    
         UpdateFolders(userConfig, "dstFolders", 0, @"C:\Temp", @"C:\Users\Darrel", @"C:\Users\Darrel\MyDocuments"); 
         UpdateFolders(userConfig, "srcFolders", 0, @"C:\Temp", @"C:\Users\Angela", @"C:\Users\Angela\MyDocuments"); 
    
        } 
    
        public void UpdateFolders(Configuration config, string sectionName, int selectedIndex, params string[] folders) 
        { 
         bool addSection = false; 
    
         var section = config.GetSection(sectionName) as FolderSection; 
         if (section == null) 
         { 
          section = new FolderSection(); 
          section.SectionInformation.AllowDefinition = ConfigurationAllowDefinition.Everywhere; 
          section.SectionInformation.AllowExeDefinition = ConfigurationAllowExeDefinition.MachineToLocalUser; 
          section.SectionInformation.ForceSave = true; 
          section.SectionInformation.ForceDeclaration(true); 
          addSection = true; 
         } 
    
         int index = 0; 
         section.Elements.EmitClear = true; 
         foreach (var folder in folders) 
         { 
          string Name = "folder" + (index + 1).ToString(); 
          // string label = FileHelper.GetVolumeLabel(folder); 
          string label = "OS"; 
          var element = section.Elements.GetElementByName(Name) as FolderElement; 
          if (element != null) 
          { 
           element.Path = folder; 
           element.VolumeLabel = label; 
          } 
          else 
          { 
           element = new FolderElement() { Name = Name, Path = folder, VolumeLabel = label }; 
           section.Elements.AddElement(element); 
          } 
          element.Selected = (selectedIndex == index); 
          index++; 
         } 
    
         // Add elements to section before adding section to config.Sections. 
         if (addSection) config.Sections.Add(sectionName, section); 
    
         config.Save(); 
    
        } 
    } 
    

    }

  • +0

    エラーは何ですか?あなたのコードのどこにエラーがありますか – cdkMoose

    +0

    @cdkMoose完全なテストケースで更新されました。 2番目の呼び出しでconfig.Save()からスローされたタイトルにエラーが表示されます。 –

    答えて

    0

    私はそれが一度だけ実行されるように更新クラスの外に保存ホイスト場合、私は、少なくともテストケースで問題を解決することができます。

    [TestMethod] 
        public void SaveConfigTest() 
        { 
         var fileMap = GetFileMap(); 
         var userConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.PerUserRoamingAndLocal); 
    
         UpdateFolders(userConfig, "dstFolders", 0, @"C:\Temp", @"C:\Users\Darrel", @"C:\Users\Darrel\MyDocuments"); 
         UpdateFolders(userConfig, "srcFolders", 0, @"C:\Temp", @"C:\Users\Angela", @"C:\Users\Angela\MyDocuments"); 
    
         userconfig.Save(); 
        } 
    

    設定を再度開いてもテストケースでは動作しているようですが、元のコードで行っていたことを誓うことができます。

    [TestMethod] 
        public void SaveConfigTest() 
        { 
         var fileMap = GetFileMap(); 
         var userConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.PerUserRoamingAndLocal); 
    
         UpdateFolders(userConfig, "dstFolders", 0, @"C:\Temp", @"C:\Users\Darrel", @"C:\Users\Darrel\MyDocuments"); 
         userconfig.Save(); 
    
         userConfig = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.PerUserRoamingAndLocal); 
         UpdateFolders(userConfig, "srcFolders", 0, @"C:\Temp", @"C:\Users\Angela", @"C:\Users\Angela\MyDocuments"); 
         userconfig.Save(); 
        } 
    
    関連する問題