public void Remove(UrlConfigElement url) { if (BaseIndexOf(url) >= 0) { BaseRemove(url.Name); } }
// </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()); }
// </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()); } }
// </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."); } }
public UrlsCollection() { // Add one url to the collection. This is // not necessary; could leave the collection // empty until items are added to it outside // the constructor. UrlConfigElement url = (UrlConfigElement)CreateNewElement(); Add(url); }
// </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()); } }
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()); }
// </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); }
public void Add(UrlConfigElement url) { BaseAdd(url); // Add custom code here. }
public int IndexOf(UrlConfigElement url) { return(BaseIndexOf(url)); }