// </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()); } }
// </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()); } }
// <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()); } }