// </Snippet6> // <Snippet7> // Show how to use IsReadOnly. // It loops to see if the elements are read only. static void ReadOnlyElements() { try { // Get the configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the MyUrls section. UrlsSection myUrlsSection = config.GetSection("MyUrls") as UrlsSection; UrlsCollection elements = myUrlsSection.Urls; IEnumerator elemEnum = elements.GetEnumerator(); int i = 0; Console.WriteLine(elements.Count.ToString()); while (elemEnum.MoveNext()) { Console.WriteLine("The element {0} is read only: {1}", elements[i].Name, elements[i].IsReadOnly().ToString()); i += 1; } } catch (ConfigurationErrorsException err) { Console.WriteLine("[ReadOnlyElements: {0}]", err.ToString()); } }
// </Snippet84> // <Snippet85> static public void GetElementProperties() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Get the element. UrlConfigElement url = section.Simple; // Get the element properties. PropertyInformationCollection properties = url.ElementInformation.Properties; foreach (PropertyInformation prop in properties) { Console.WriteLine( "Name: {0} Type: {1}", prop.Name, prop.Type.ToString()); } }
static void Main(string[] args) { // 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 { Console.WriteLine("The 'simple' element of app.config:"); Console.WriteLine(" Name={0} URL={1} Port={2}", myUrlsSection.Simple.Name, myUrlsSection.Simple.Url, myUrlsSection.Simple.Port); Console.WriteLine("The urls collection of app.config:"); for (int i = 0; i < myUrlsSection.Urls.Count; i++) { Console.WriteLine(" Name={0} URL={1} Port={2}", myUrlsSection.Urls[i].Name, myUrlsSection.Urls[i].Url, myUrlsSection.Urls[i].Port); } } Console.ReadLine(); }
// </Snippet93> // <Snippet94> static public void ProtectSection() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Protect (encrypt)the section. section.SectionInformation.ProtectSection( "RsaProtectedConfigurationProvider"); // Save the encrypted section. section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); // Display decrypted configuration // section. Note, the system // uses the Rsa provider to decrypt // the section transparently. string sectionXml = section.SectionInformation.GetRawXml(); Console.WriteLine("Decrypted section:"); Console.WriteLine(sectionXml); }
// </Snippet1> static void AddClearRemoveElementName() { try { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the configuration section MyUrls. UrlsSection myUrlsSection = config.Sections["MyUrls"] as UrlsSection; // Get the configuration element collection. UrlsCollection elements = myUrlsSection.Urls; Console.WriteLine("Default Add name: {0}", elements.AddElementName); Console.WriteLine("Default Remove name: {0}", elements.RemoveElementName); Console.WriteLine("Default Clear name: {0}", elements.ClearElementName); } catch (ConfigurationErrorsException e) { Console.WriteLine("[AddElementName: {0}]", e.ToString()); } }
// </Snippet80> // <Snippet81> static public void IsElementCollection() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Get the element. UrlConfigElement url = section.Simple; // Get the collection element. UrlsCollection urls = section.Urls; bool isCollection = url.ElementInformation.IsCollection; Console.WriteLine("Url element is a collection? {0}", isCollection.ToString()); isCollection = urls.ElementInformation.IsCollection; Console.WriteLine("Urls element is a collection? {0}", isCollection.ToString()); }
// </Snippet111> // <Snippet112> static public void UnProtectSection() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Unprotect (decrypt)the section. section.SectionInformation.UnprotectSection(); // <Snippet113> // Force the section information to be written to // the configuration file. section.SectionInformation.ForceDeclaration(true); // </Snippet113> // Save the decrypted section. section.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); // Display the decrypted configuration // section. string sectionXml = section.SectionInformation.GetRawXml(); Console.WriteLine("Decrypted section:"); Console.WriteLine(sectionXml); }
// </Snippet7> // Remove a UrlConfigElement from the collection. static void RemoveElement(string name, string url, int port) { // Get the configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the MyUrls section. UrlsSection myUrlsSection = config.GetSection("MyUrls") as UrlsSection; UrlsCollection urls = myUrlsSection.Urls; UrlConfigElement element = new UrlConfigElement(name, url, port); if (!myUrlsSection.ElementInformation.IsLocked) { myUrlsSection.Urls.Remove(element); config.Save(ConfigurationSaveMode.Minimal); // This to obsolete the cached section and // read the new updated one. ConfigurationManager.RefreshSection("MyUrls"); } else { Console.WriteLine( "Section was locked, could not update."); } }
// </Snippet5> // <Snippet6> // Show how to use IsModified. // This method modifies the port property // of the url element named Microsoft and // saves the modification to the configuration // file. This in turn will cause the overriden // UrlConfigElement.IsModified() mathod to be called. static void ModifyElement() { try { // Get the configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the MyUrls section. UrlsSection myUrlsSection = config.GetSection("MyUrls") as UrlsSection; UrlsCollection elements = myUrlsSection.Urls; IEnumerator elemEnum = elements.GetEnumerator(); int i = 0; while (elemEnum.MoveNext()) { if (elements[i].Name == "Microsoft") { elements[i].Port = 1010; bool readOnly = elements[i].IsReadOnly(); break; } i += 1; } if (!myUrlsSection.ElementInformation.IsLocked) { config.Save(ConfigurationSaveMode.Full); // This to obsolete the MyUrls cached // section and read the updated version // from the configuration file. ConfigurationManager.RefreshSection("MyUrls"); } else { Console.WriteLine( "Section was locked, could not update."); } } catch (ConfigurationErrorsException err) { Console.WriteLine("[ModifyElement: {0}]", err.ToString()); } }
// </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()); } }
// </Snippet2> // <Snippet3> // Show how to set LockItem // It adds a new UrlConfigElement to // the collection. static void LockItem() { string name = "Contoso"; string url = "http://www.contoso.com/"; int port = 8080; try { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the MyUrls section. UrlsSection myUrls = config.Sections["MyUrls"] as UrlsSection; // Create the new element. UrlConfigElement newElement = new UrlConfigElement(name, url, port); // Set its lock. newElement.LockItem = true; // Save the new element to the // configuration file. if (!myUrls.ElementInformation.IsLocked) { myUrls.Urls.Add(newElement); config.Save(ConfigurationSaveMode.Full); // This is used to obsolete the cached // section and read the updated // bersion from the configuration file. ConfigurationManager.RefreshSection("MyUrls"); } else { Console.WriteLine( "Section was locked, could not update."); } } catch (ConfigurationErrorsException e) { Console.WriteLine("[LockItem: {0}]", e.ToString()); } }
// </Snippet100> static public void GetIsProperties() { // <Snippet102> // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); SectionInformation sInfo = section.SectionInformation; // </Snippet102> // <Snippet103> bool declRequired = sInfo.IsDeclarationRequired; Console.WriteLine("Declaration required?: {0}", declRequired.ToString()); // </Snippet103> // <Snippet104> bool declared = sInfo.IsDeclared; Console.WriteLine("Section declared?: {0}", declared.ToString()); // </Snippet104> // <Snippet105> bool locked = sInfo.IsLocked; Console.WriteLine("Section locked?: {0}", locked.ToString()); // </Snippet105> // <Snippet106> bool protect = sInfo.IsProtected; Console.WriteLine("Section protected?: {0}", protect.ToString()); // </Snippet106> }
// </Snippet94> static public void GetAllowProperties() { // <Snippet95> // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); SectionInformation sInfo = section.SectionInformation; // </Snippet95> // <Snippet96> ConfigurationAllowDefinition allowDefinition = sInfo.AllowDefinition; Console.WriteLine("Allow definition: {0}", allowDefinition.ToString()); // </Snippet96> // <Snippet97> ConfigurationAllowExeDefinition allowExeDefinition = sInfo.AllowExeDefinition; Console.WriteLine("Allow exe definition: {0}", allowExeDefinition.ToString()); // </Snippet97> // <Snippet98> bool allowLocation = sInfo.AllowLocation; Console.WriteLine("Allow location: {0}", allowLocation.ToString()); // </Snippet98> // <Snippet99> bool allowOverride = sInfo.AllowOverride; Console.WriteLine("Allow override: {0}", allowOverride.ToString()); // </Snippet99> }
GetSectionInformation() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); SectionInformation sInfo = section.SectionInformation; return(sInfo); }
// <Snippet2> // Show the use of Properties. // It displays the ConfigurationElement // properties. static void GetProperties() { try { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the configuration section MyUrls. UrlsSection myUrlsSection = config.Sections["MyUrls"] as UrlsSection; // Get the configuration element collection. UrlsCollection elements = myUrlsSection.Urls; IEnumerator elemEnum = elements.GetEnumerator(); int i = 0; while (elemEnum.MoveNext()) { // Get the current element configuration // property collection. PropertyInformationCollection properties = elements[i].ElementInformation.Properties; // Display the current configuration // element properties. foreach (PropertyInformation property in properties) { Console.WriteLine("Name: {0}\tDefault: {1}\tRequired: {2}", property.Name, property.DefaultValue, property.IsRequired.ToString()); } } } catch (ConfigurationErrorsException e) { Console.WriteLine("[GetProperties: {0}]", e.ToString()); } }
GetElementInformation() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Get the element. UrlConfigElement url = section.Simple; ElementInformation eInfo = url.ElementInformation; return(eInfo); }
// </Snippet82> // <Snippet83> static public void IsElementPresent() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Get the element. UrlConfigElement url = section.Simple; bool isPresent = url.ElementInformation.IsPresent; Console.WriteLine("Url element is present? {0}", isPresent.ToString()); }
// </Snippet83> // <Snippet84> static public void GetElementLineNumber() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Get the collection element. UrlsCollection urls = section.Urls; int ln = urls.ElementInformation.LineNumber; Console.WriteLine("Urls element line number: {0}", ln.ToString()); }
// </Snippet88> // <Snippet89> static public void GetElementErrors() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Get the element. UrlConfigElement url = section.Simple; // Get the errors. ICollection errors = url.ElementInformation.Errors; Console.WriteLine("Number of errors: {0)", errors.Count.ToString()); }
// </Snippet87> // <Snippet88> static public void GetElementValidator() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Get the element. UrlConfigElement url = section.Simple; // Get the element source file. ConfigurationValidatorBase elValidator = url.ElementInformation.Validator; Console.WriteLine("Url element validator: {0}", elValidator.ToString()); }
// </Snippet86> // <Snippet87> static public void GetElementType() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Get the element. UrlConfigElement url = section.Simple; // Get the element type. Type elType = url.ElementInformation.Type; Console.WriteLine("Url element type: {0}", elType.ToString()); }
// </Snippet85> // <Snippet86> static public void GetElementSource() { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); // Get the section. UrlsSection section = (UrlsSection)config.GetSection("MyUrls"); // Get the element. UrlConfigElement url = section.Simple; // Get the element source file. string sourceFile = url.ElementInformation.Source; Console.WriteLine("Url element source file: {0}", sourceFile); }
// <Snippet1> // Create a section whose name is // MyUrls that contains a nested collection as // defined by the UrlsSection class. static void CreateSection() { string sectionName = "MyUrls"; try { // Get the current configuration file. System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None); UrlsSection urlsSection; // Create the section whose name attribute // is MyUrls in <configSections>. // Also, create the related target section // MyUrls in <configuration>. if (config.Sections[sectionName] == null) { urlsSection = new UrlsSection(); // Change the default values of // the simple element. urlsSection.Simple.Name = "Contoso"; urlsSection.Simple.Url = "http://www.contoso.com"; urlsSection.Simple.Port = 8080; config.Sections.Add(sectionName, urlsSection); urlsSection.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } catch (ConfigurationErrorsException e) { Console.WriteLine("[CreateSection: {0}]", e.ToString()); } }
// </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()); } }