none
How to remove Version, Culture and PublicKeyToken attribute from the ConfigSection RRS feed

  • Question

  • I created custom configuration file like following.

    <configSections>
        <section name="deviceConfiguration" type="Test.MyDeviceConfiguration, Test, Version=8.1.5.0, Culture=neutral, PublicKeyToken=null" allowLocation="true" allowDefinition="Everywhere"/>
    </configSections>

    I want to remove  Version, Culture and PublicKeyToken attribute from the ConfigSection.

    How could I do that?

    Friday, December 28, 2018 3:31 PM

Answers

  • Hi Jeff0803,

    Based on my testing, it seems that configuration object does not provide a method to remove Version, Culture and PublicKeyToken attribute from the ConfigSection remove. 

    I would suggest that you remove them by manually.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, January 3, 2019 2:24 AM
    Moderator

All replies

  • Just remove it from the string.

    <configSections>
       <section name="deviceConfiguration" type="Test.MyDeviceConfiguration, Test" />
    </configSections>
    Note that for this to work the assembly would need to be in the directory (not GACced). Also if the section is the same as the application then you don't need the assembly name either.


    Michael Taylor http://www.michaeltaylorp3.net

    Friday, December 28, 2018 6:41 PM
    Moderator
  • My program create custom configuration file at run-time.

    I wanted to create configuration file's configSection without Version, Culture and PublicKeyToken.

    Saturday, December 29, 2018 5:05 PM
  • "My program create custom configuration file at run-time."

    What? The configuration subsystem is loaded early in the .NET process. Custom configuration sections have to be defined before the subsystem initializes. You cannot dynamically add new sections at runtime (at least for the current app). Even if the subsystem allowed it you wouldn't be able to generate a dynamic assembly that could then be read by the configuration subsystem (because it is already loaded).

    What exactly are you trying to do? Are you sure the app config file is the correct place for this logic? It sounds like you really need your own config (XML, JSON, whatever) file and not the app config.


    Michael Taylor http://www.michaeltaylorp3.net

    Saturday, December 29, 2018 5:53 PM
    Moderator
  • Hi Jeff0803,

    Could you please share a bit more information about the issue, such as related code, the steps about how to reproduce the issue.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 31, 2018 2:36 AM
    Moderator
  • Here is the code snippet of my program.

    public bool CreateMyDefaultConfigFile(string configfile)
    {
        ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
        configFileMap.ExeConfigFilename = configfile;
        try
        {
            // OpenApp.Config of executable
            Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
            // Add an Application Setting if not exist
            config.AppSettings.Settings.Add("~~~~~~~~", "~~");
            config.AppSettings.Settings.Add("~~~~~~~~", "~~");
            DeviceConfiguration section1 = new DeviceConfiguration();
            config.Sections.Add("MySection", section1);
            config.Sections["deviceConfiguration"].SectionInformation.AllowLocation = true;
            config.Sections["deviceConfiguration"].SectionInformation.AllowDefinition = ConfigurationAllowDefinition.Everywhere;
            config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("configuration");
        }
        catch (ConfigurationErrorsException ex)
        {
            MessageBox.Show("Failed to create configuration file." + Environment.NewLine +
                            ex.Message);
            return false;
        }
        return true;
    }
    

    Monday, December 31, 2018 4:45 PM
  • I create my own configuration file.

    Please reference to the code in the answer per Zhanlong Wu below.


    • Edited by Jeff0803 Monday, December 31, 2018 4:47 PM
    Monday, December 31, 2018 4:47 PM
  • Hi Jeff0803,

    Based on your sample, I could not find the class named DeviceConfiguration, how to get it. install it via Nuget or add related reference?

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, January 1, 2019 5:53 AM
    Moderator
  • It was discribed in the config.AppSettings.Add("~~~~~","~~").

    I just didn't want to expose it in this article because it shows our company name.

    Wednesday, January 2, 2019 8:02 PM
  • Hi Jeff0803,

    Based on my testing, it seems that configuration object does not provide a method to remove Version, Culture and PublicKeyToken attribute from the ConfigSection remove. 

    I would suggest that you remove them by manually.

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, January 3, 2019 2:24 AM
    Moderator