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