public void Populate_Error() { SysConfig cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationLockCollection col = cfg.AppSettings.LockAttributes; col.Add("boo"); }
public void Populate_Error() { SysConfig cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationLockCollection col = cfg.AppSettings.LockAttributes; Assert.Throws <ConfigurationErrorsException>(() => col.Add("boo")); }
public void Populate() { SysConfig cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationLockCollection col = cfg.AppSettings.LockAttributes; col.Add("file"); Assert.AreEqual(1, col.Count, "A1"); Assert.IsFalse(col.HasParentElements, "A2"); Assert.IsTrue(col.IsModified, "A3"); Assert.IsTrue(col.Contains("file"), "A4"); }
// </Snippet4> // <Snippet5> // Show how to use LockAllElementsExcept. // It locks and unlocks all the MyUrls elements // except urls. static void LockAllElementsExcept() { try { // Get the configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the MyUrls section. UrlsSection myUrlsSection = config.GetSection("MyUrls") as UrlsSection; if (myUrlsSection == null) { Console.WriteLine("Failed to load UrlsSection."); } else { // Get MyUrls section LockElements collection. ConfigurationLockCollection lockElementsExcept = myUrlsSection.LockAllElementsExcept; // Get MyUrls section LockElements collection // enumerator. IEnumerator lockElementEnum = lockElementsExcept.GetEnumerator(); // Position the collection index. lockElementEnum.MoveNext(); if (lockElementsExcept.Contains("urls")) { // Remove the lock on all the ther elements. lockElementsExcept.Remove("urls"); } else { // Add the lock on all the other elements // but urls element. lockElementsExcept.Add("urls"); } config.Save(ConfigurationSaveMode.Full); } } catch (ConfigurationErrorsException err) { Console.WriteLine("[LockAllElementsExcept: {0}]", err.ToString()); } }
public void Enumerator() { SysConfig cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationLockCollection col = cfg.AppSettings.LockAttributes; col.Add("file"); IEnumerator e = col.GetEnumerator(); Assert.IsTrue(e.MoveNext(), "A1"); Assert.AreEqual("file", (string)e.Current, "A2"); Assert.IsFalse(e.MoveNext(), "A3"); }
public void SetFromList() { SysConfig cfg = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); ConfigurationLockCollection col = cfg.AppSettings.LockAttributes; col.SetFromList("file"); Assert.AreEqual(1, col.Count, "A1"); Assert.IsTrue(col.Contains("file"), "A2"); col.Clear(); Assert.AreEqual(0, col.Count, "A5"); col.SetFromList(" file "); Assert.AreEqual(1, col.Count, "A1"); Assert.IsTrue(col.Contains("file"), "A2"); }
// </Snippet8> // <Snippet9> // Show how to use LockAllAttributesExcept. // It locks and unlocks all urls elements // except the port. static void LockAllAttributesExcept() { try { // Get current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the MyUrls section. UrlsSection myUrlsSection = config.GetSection("MyUrls") as UrlsSection; if (myUrlsSection == null) { Console.WriteLine( "Failed to load UrlsSection."); } else { IEnumerator elemEnum = myUrlsSection.Urls.GetEnumerator(); int i = 0; while (elemEnum.MoveNext()) { // Get current element. ConfigurationElement element = myUrlsSection.Urls[i]; // Get current element lock all attributes. ConfigurationLockCollection lockAllAttributesExcept = element.LockAllAttributesExcept; // Add or remove the lock on all attributes // except port. if (lockAllAttributesExcept.Contains("port")) { lockAllAttributesExcept.Remove("port"); } else { lockAllAttributesExcept.Add("port"); } string lockedAttributes = lockAllAttributesExcept.AttributeList; Console.WriteLine( "Element {0} Locked attributes list: {1}", i.ToString(), lockedAttributes); i += 1; config.Save(ConfigurationSaveMode.Full); } } } catch (ConfigurationErrorsException e) { Console.WriteLine( "[LockAllAttributesExcept: {0}]", e.ToString()); } }