public void Remove(UrlConfigElement url)
 {
     if (BaseIndexOf(url) >= 0)
     {
         BaseRemove(url.Name);
     }
 }
 //<Snippet3>
 public void Remove(UrlConfigElement url)
 {
     if (BaseIndexOf(url) >= 0)
     {
         BaseRemove(url.Name);
         // Your custom code goes here.
         Console.WriteLine("UrlsCollection: {0}", "Removed collection element!");
     }
 }
Ejemplo n.º 3
0
    // Create a custom section and save it in the
    // application configuration file.
    static void CreateCustomSection()
    {
        try
        {
            // Get the current configuration file.
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None);

            // Add the custom section to the application
            // configuration file.
            UrlsSection myUrlsSection = (UrlsSection)config.Sections["MyUrls"];

            if (myUrlsSection == null)
            {
                //  The configuration file does not contain the
                // custom section yet. Create it.
                myUrlsSection = new UrlsSection();

                config.Sections.Add("MyUrls", myUrlsSection);

                // Save the application configuration file.
                myUrlsSection.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Modified);
            }
            else
            if (myUrlsSection.Urls.Count == 0)
            {
                // The configuration file contains the
                // custom section but its element collection is empty.
                // Initialize the collection.
                UrlConfigElement url = new UrlConfigElement();
                myUrlsSection.Urls.Add(url);

                // Save the application configuration file.
                myUrlsSection.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Modified);
            }


            Console.WriteLine("Created custom section in the application configuration file: {0}",
                              config.FilePath);
            Console.WriteLine();
        }
        catch (ConfigurationErrorsException err)
        {
            Console.WriteLine("CreateCustomSection: {0}", err.ToString());
        }
    }
Ejemplo n.º 4
0
    // Add an element to the custom section collection.
    // This function uses the ConfigurationCollectionElement Add method.
    static void AddCollectionElement()
    {
        try
        {
            // Get the current configuration file.
            System.Configuration.Configuration config =
                ConfigurationManager.OpenExeConfiguration(
                    ConfigurationUserLevel.None);


            // Get the custom configuration section.
            UrlsSection myUrlsSection = config.GetSection("MyUrls") as UrlsSection;


            // Add the element to the collection in the custom section.
            if (config.Sections["MyUrls"] != null)
            {
                UrlConfigElement urlElement = new UrlConfigElement();
                urlElement.Name = "Microsoft";
                urlElement.Url  = "http://www.microsoft.com";
                urlElement.Port = 8080;

                // Use the ConfigurationCollectionElement Add method
                // to add the new element to the collection.
                myUrlsSection.Urls.Add(urlElement);


                // Save the application configuration file.
                myUrlsSection.SectionInformation.ForceSave = true;
                config.Save(ConfigurationSaveMode.Modified);


                Console.WriteLine("Added collection element to the custom section in the configuration file: {0}",
                                  config.FilePath);
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("You must create the custom section first.");
            }
        }
        catch (ConfigurationErrorsException err)
        {
            Console.WriteLine("AddCollectionElement: {0}", err.ToString());
        }
    }
 public void Add(UrlConfigElement url)
 {
     BaseAdd(url);
 }
 public int IndexOf(UrlConfigElement url)
 {
     return(BaseIndexOf(url));
 }
    public UrlsCollection()
    {
        UrlConfigElement url = (UrlConfigElement)CreateNewElement();

        Add(url);
    }
    // Create a new instance of the UrlsSection.
    // This constructor creates a configuration element
    // using the UrlConfigElement default values.
    // It assigns this element to the collection.
    public UrlsSection()
    {
        UrlConfigElement url = new UrlConfigElement();

        Urls.Add(url);
    }
    //<Snippet2>
    public void Add(UrlConfigElement url)
    {
        BaseAdd(url);

        // Your custom code goes here.
    }