Click here to Skip to main content
15,894,343 members
Home / Discussions / .NET (Core and Framework)
   

.NET (Core and Framework)

 
AnswerRe: Understanding using statement Pin
supercat911-Sep-09 8:02
supercat911-Sep-09 8:02 
AnswerRe: Understanding using statement Pin
muktaa15-Sep-09 1:12
muktaa15-Sep-09 1:12 
AnswerRe: Understanding using statement Pin
Shameel22-Oct-09 5:37
professionalShameel22-Oct-09 5:37 
QuestionConfigurationManager: ConfigurationErrorsException after removing config attribute? [RESOLVED] Pin
Homncruse8-Sep-09 13:17
Homncruse8-Sep-09 13:17 
AnswerRe: ConfigurationManager: ConfigurationErrorsException after removing config attribute? Pin
Shameel22-Oct-09 5:41
professionalShameel22-Oct-09 5:41 
GeneralRe: ConfigurationManager: ConfigurationErrorsException after removing config attribute? Pin
Homncruse22-Oct-09 6:36
Homncruse22-Oct-09 6:36 
GeneralRe: ConfigurationManager: ConfigurationErrorsException after removing config attribute? Pin
Shameel22-Oct-09 8:56
professionalShameel22-Oct-09 8:56 
GeneralRe: ConfigurationManager: ConfigurationErrorsException after removing config attribute? Pin
Homncruse22-Oct-09 9:34
Homncruse22-Oct-09 9:34 
It's a Windows app using the built-in ConfigurationManager .NET capabilities. I based it on this MSDN code sample[^]. Maybe it'll be easier to just show some code:

Configuration classes:
public class SerialConfig : ConfigurationElement
    {
        /* *********************************** */
        /* This is the field I want to remove! */
        /* *********************************** */      
        // Fields
        [ConfigurationProperty("valid",
            DefaultValue = false,
            IsRequired = false)]
        //private int baud;
        public bool Valid
        {
            get
            {
                return (bool)this["valid"];
            }
            set
            {
                this["valid"] = (bool)value;
            }
        }


        [ConfigurationProperty("baud",
            DefaultValue = 115200,
            IsRequired = true)]
        //private int baud;
        public int Baud
        {
            get
            {
                return (int)this["baud"];
            }
            set
            {
                if((value == 115200) || (value == 9600))
                {
                    this["baud"] = value;
                }
                else
                {
                }
            }
        }

        [ConfigurationProperty("name",
            DefaultValue = "",
            IsRequired = true)]
        //private String name;
        public String Name
        {
            get
            {
                return (string)this["name"];
            }
            set
            {
                if (value.Contains("COM"))
                {
                    this["name"] = value;
                }
                else
                {
                    this["name"] = "INVALID";
                }
            }
        }
        
        // Constructors
        public SerialConfig(String name, int baud)
        {
            this["baud"] = baud;
            this["name"] = name;            
        }

        public SerialConfig(string name)
        {
            new SerialConfig(name, 115200);
        }

        public SerialConfig()
        {
            new SerialConfig("INVALID", 115200);
        }
    }

    // Define a custom section that is used by the application
    // to create custom configuration sections at the specified 
    // level in the configuration hierarchy that is in the 
    // proper configuration file.
    // This enables the user that has the proper access 
    // rights, to make changes to the configuration files.
    public class SerialConfigSection : ConfigurationSection
    {
        // Create a configuration section.
        public SerialConfigSection()
        {
            this["serialConfig"] = new SerialConfig();
        }

        // Set or get the SerialConfig. 
        [ConfigurationProperty("serialConfig")]
        public SerialConfig serialConfig
        {
            get
            {
                return (
                  (SerialConfig)this["serialConfig"]);
            }
            set
            {
                this["serialConfig"] = value;
            }
        }
    }

Actual usage of configuration
/// <summary>
/// Reads stored configuration settings from
/// ConfigurationUserLevel.PerUserRoamingAndLocal,
/// creating new default entries if corrupted or not present.
/// </summary>
public static void InitializeConfig()
{
    // Get the configuration that applies to the current user.
    Configuration userConfig =
        ConfigurationManager.OpenExeConfiguration(
            ConfigurationUserLevel.PerUserRoamingAndLocal);

    // Map the configuration file. This enables the application to access
    // the configuration file using the System.Configuration.Configuration
    // class
    ExeConfigurationFileMap configFileMap =
        new ExeConfigurationFileMap();
    configFileMap.ExeConfigFilename =
        userConfig.FilePath;

    // Get the mapped configuration file.
    config =
        ConfigurationManager.OpenMappedExeConfiguration(
            configFileMap, ConfigurationUserLevel.None);

    // Try to read the config file settings for the serial ports
    EITPortConfig = null;
    BridgePortConfig = null;

    try
    {
        EITPortConfig = (SerialConfigSection)config.GetSection("EITCOMPort");
        BridgePortConfig = (SerialConfigSection)config.GetSection("BridgeCOMPort");

        // Synchronize the application configuration
        // if needed. The following two steps seem
        // to solve some out of synch issues
        // between roaming and default
        // configuration.
        config.Save(ConfigurationSaveMode.Modified);

        // Force a reload of the changed section,
        // if needed. This makes the new values available
        // for reading.
        ConfigurationManager.RefreshSection("EITCOMPort");
        ConfigurationManager.RefreshSection("BridgeCOMPort");

        // If we couldn't retrieve the EITPort section from the config
        if (EITPortConfig == null)
        {
            EITPortConfig = new SerialConfigSection();

            // Define where in the configuration file
            // hierarchy the associated
            // configuration section can be declared.
            // The following assignment assures that
            // the configuration information can be
            // defined in the user.config file in the
            // roaming user directory.
            EITPortConfig.SectionInformation.AllowExeDefinition =
              ConfigurationAllowExeDefinition.MachineToLocalUser;

            // Allow the configuration section to be
            // overridden by lower-level configuration files.
            // This means that lower-level files can contain
            // the section (use the same name) and assign
            // different values to it.
            EITPortConfig.SectionInformation.AllowOverride = true;

            // Add configuration information to
            // the configuration file.
            config.Sections.Add("EITCOMPort", EITPortConfig);
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of the changed section. This
            // makes the new values available for reading.
            ConfigurationManager.RefreshSection("EITCOMPort");
        }

        // If we couldn't retrieve the EITPort section from the config
        if (BridgePortConfig == null)
        {
            BridgePortConfig = new SerialConfigSection();

            // Define where in the configuration file
            // hierarchy the associated
            // configuration section can be declared.
            // The following assignment assures that
            // the configuration information can be
            // defined in the user.config file in the
            // roaming user directory.
            BridgePortConfig.SectionInformation.AllowExeDefinition =
              ConfigurationAllowExeDefinition.MachineToLocalUser;

            // Allow the configuration section to be
            // overridden by lower-level configuration files.
            // This means that lower-level files can contain
            // the section (use the same name) and assign
            // different values to it.
            BridgePortConfig.SectionInformation.AllowOverride = true;

            // Add configuration information to
            // the configuration file.
            config.Sections.Add("BridgeCOMPort", BridgePortConfig);
            config.Save(ConfigurationSaveMode.Modified);
            // Force a reload of the changed section. This
            // makes the new values available for reading.
            ConfigurationManager.RefreshSection("BridgeCOMPort");
        }

    }
    catch (ConfigurationErrorsException e)
    {
        Console.WriteLine("[Exception error: {0}]", e.ToString());
    }
}

GeneralRe: ConfigurationManager: ConfigurationErrorsException after removing config attribute? Pin
Shameel23-Oct-09 8:23
professionalShameel23-Oct-09 8:23 
GeneralRe: ConfigurationManager: ConfigurationErrorsException after removing config attribute? Pin
Shameel23-Oct-09 8:25
professionalShameel23-Oct-09 8:25 
GeneralRe: ConfigurationManager: ConfigurationErrorsException after removing config attribute? Pin
Homncruse23-Oct-09 8:41
Homncruse23-Oct-09 8:41 
QuestionPartial loading of .net assemblies Pin
Jaime Olivares7-Sep-09 20:53
Jaime Olivares7-Sep-09 20:53 
AnswerRe: Partial loading of .net assemblies [modified] Pin
Eddy Vluggen7-Sep-09 21:12
professionalEddy Vluggen7-Sep-09 21:12 
GeneralRe: Partial loading of .net assemblies Pin
Jaime Olivares7-Sep-09 21:21
Jaime Olivares7-Sep-09 21:21 
GeneralRe: Partial loading of .net assemblies Pin
Eddy Vluggen7-Sep-09 21:45
professionalEddy Vluggen7-Sep-09 21:45 
GeneralRe: Partial loading of .net assemblies Pin
Jaime Olivares8-Sep-09 3:03
Jaime Olivares8-Sep-09 3:03 
GeneralRe: Partial loading of .net assemblies Pin
Not Active8-Sep-09 3:15
mentorNot Active8-Sep-09 3:15 
GeneralRe: Partial loading of .net assemblies Pin
Jaime Olivares9-Sep-09 3:03
Jaime Olivares9-Sep-09 3:03 
GeneralRe: Partial loading of .net assemblies Pin
Not Active9-Sep-09 3:39
mentorNot Active9-Sep-09 3:39 
AnswerRe: Partial loading of .net assemblies Pin
Not Active8-Sep-09 2:13
mentorNot Active8-Sep-09 2:13 
GeneralRe: Partial loading of .net assemblies Pin
Jaime Olivares8-Sep-09 3:00
Jaime Olivares8-Sep-09 3:00 
GeneralRe: Partial loading of .net assemblies Pin
Not Active8-Sep-09 3:13
mentorNot Active8-Sep-09 3:13 
GeneralRe: Partial loading of .net assemblies Pin
Jaime Olivares9-Sep-09 2:58
Jaime Olivares9-Sep-09 2:58 
GeneralRe: Partial loading of .net assemblies Pin
Not Active9-Sep-09 3:36
mentorNot Active9-Sep-09 3:36 
GeneralRe: Partial loading of .net assemblies Pin
Dave Kreskowiak9-Sep-09 6:33
mveDave Kreskowiak9-Sep-09 6:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.