locked
How to correctly access application settings in C# using reflection RRS feed

  • Question

  • Hi,

    I am trying to access application properties using reflection with the following code:

          List<StringDictionary> dictList = new List<StringDictionary>();
          StringCollection bogus = new StringCollection();
          foreach (PropertyInfo info in Properties.Settings.Default.GetType().GetProperties())
          {
            if (!("logLevel".Equals(info.Name) || "eventURL".Equals(info.Name)))
            {
              if (bogus.GetType().IsAssignableFrom(info.PropertyType))
              {
                StringCollection rawConfig = (StringCollection)info.GetValue(Properties.Settings.Default, null);
                // do something
              }
            }
          }
    
    

    The code "works" in that it successfully gets the right properties.   However, it does not work properly when I deploy the app and change myapp.exe.config.  When I change the config file then properties I access normally do reflect the .config file.  For example, this call picks up the values in myapp.exe.config.

     String logLevelStr = Properties.Settings.Default.logLevel
    
    What am I doing wrong in my reflection code that causes me to pull only the properties defined at design time and not what is currently in myapp.exe.config?

    • Moved by Leo Liu - MSFT Friday, April 22, 2011 1:57 AM Off Topic (From:Visual C# General)
    Friday, April 15, 2011 8:05 AM

All replies

  • Hi, could not figure out any thing abnormal from your code.

    BTY, what is your deploy method? Click Once or msi?
    For both these methods you should consider about whether you have authority to access the folder.

    >> "it does not work properly when I deploy the app and change myapp.exe.config"
    And how? How does it work improperly? Any exception is thrown? If not please add try...catch block to see if any exception exists, as for file operation, it is always recommended to perform this.

    If no response is got, I'd like to suggest that you post your thread in the Windows Forms General Forum, then I will move your thread into the Off-Topic Forum shortly, Thanks for your understanding. 
    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Monday, April 18, 2011 9:46 AM