none
User settings in C# generates exception error when targeting .Net 3.5 or earlier RRS feed

  • Question

  • Hello,
    I get stuck with a problem, which is described
    here:
    and 
    here:
    I have an application and decided to store settings in the configuration file. I don't want to store settings in the registry.
    I've created derived class from standard settings class ("ApplicationSettingsBase") and put there my settings.
    Everything works like a charm, but unfortunately we discovered problem with settings:
    1. Create .NET 4 application, which loads at start and saves settings in the end of work.
    2. Run this application. (Settings are loaded.) Close the application. (Settings are saved.)
    3. Change target framework from 4 to 3.5. Build the project and repeat action number "2". -> You will get this exception which is not only annoying and can be caught, but I haven't found any workaround to prevent consequences of this exception. It is caused by the line in the config file (bold one):
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
    ...
    I've wrapped my method which loads setting in try{}catch block, but the problem come from save settings. I no longer can't save my settings. And there is no benefits from using config file since then.
    Does anyone know how to change this 4.0.0.0 programmatically or make a workaround to achieve the following purposes:
    1. "Try" to open settings, reset them if described problem appeared;
    2. Save settings somehow, in order to prevent loading settings crash one more time in the future.
    Thank you,
    Vitaliy Korney

    Thursday, June 16, 2011 11:29 AM

Answers

  • I understand how this might be incovenient for development, but I'm not sure how your users would ever be exposed to this issue. Care to elaborate what the use case would be here?

    Thanks
    --mc

     

    • Marked as answer by Paul Zhou Friday, July 1, 2011 5:59 AM
    Thursday, June 16, 2011 1:07 PM

All replies

  • I understand how this might be incovenient for development, but I'm not sure how your users would ever be exposed to this issue. Care to elaborate what the use case would be here?

    Thanks
    --mc

     

    • Marked as answer by Paul Zhou Friday, July 1, 2011 5:59 AM
    Thursday, June 16, 2011 1:07 PM
  • Hello Mario,

    Rather simple example of the use case:

    You are developing components for programmers and they shouldn't care about your config file which stores settings for your component. Of course if you create end-user application it's inconvenient, but can be resolved manually. In case of component it's more than inconvenient, it points that your component is unreliable.

    Thanks,

    Vitaliy Korney



    Thursday, June 16, 2011 2:39 PM
  • Hello,

    I realized that it's much more easier to make custom setting file rather than trying to create crooked nail with reflection and reflector help for this unpredictable behaviour.

    Thanks,

    Vitaliy Korney

    Friday, June 17, 2011 10:20 AM