Geeks With Blogs
Beluga Consultancy

Manipulating app.config attributes at run time in C# appears to be simple. However most of the samples I've looked at don't work or are impenetrable.

Simply in app.config file at design time add some userSettings either manually or through the design window

 

   <userSettings>
        <WindowsFormsApplication1.Properties.Settings>
            <setting name="MyMachine" serializeAs="String">
                <value>zaxcc1Machine</value>
            </setting>
            <setting name="MyMessage" serializeAs="String">
                <value>Helpme</value>
            </setting>
            <setting name="MySubText" serializeAs="String">
                <value>Imintrouble </value>
            </setting>
        </WindowsFormsApplication1.Properties.Settings>
    </userSettings>

 

Then use these values by using Properties.Settings.Default object. Note this must be prefixed by the application name

 

//Using app.config settings
txtIPMachine.Text=WindowsFormsApplication1.Properties.Settings.Default.MyMachine;
txtMessage.Text=WindowsFormsApplication1.Properties.Settings.Default.MyMessage;
txtSubtext.Text=WindowsFormsApplication1.Properties.Settings.Default.MySubText;

 

To change these settings make assignments to them 

WindowsFormsApplication1.Properties.Settings.Default.MyMachine=txtIPMachine.Text;
WindowsFormsApplication1.Properties.Settings.Default.MyMessage=txtMessage.Text;
WindowsFormsApplication1.Properties.Settings.Default.MySubText=txtSubtext.Text;

 

However to persist these changes which will change the associated .exe.config file another statement is required:

//Now persist these changes
WindowsFormsApplication1.Properties.Settings.Default.Save();

 

Only UserSetting (as opposed to AppSetting) changes are persistable at runtime.

 

Now that all looks really promising except that it doesn't actually put the persisted user settings in the appname.exe.config. Instead it puts these in a hidden file for the user in their documents and settings (something like this)

C:\Documents and Settings\FredBloggs\Local Settings\Application Data\Company\MyApp.exe_Url_ywmpcb12duakrunjye5nrdb4hhsecogv\1.2.0.7

The file is always called user.config

This will contain the xml for user settings from above and could be manually altered and will be read in at startup but it's clearly designed not to be. It's obviously also designed to allow different users to have different persisted settings.

   <userSettings>
        <WindowsFormsApplication1.Properties.Settings>
            <setting name="MyMachine" serializeAs="String">
                <value>zaxcc1Machine</value>
            </setting>
            <setting name="MyMessage" serializeAs="String">
                <value>Helpme</value>
            </setting>
            <setting name="MySubText" serializeAs="String">
                <value>Imintrouble </value>
            </setting>
        </WindowsFormsApplication1.Properties.Settings>
    </userSettings>

All I really wanted was a way of persisting some settings when the application is in use and I wanted that in the same folder as the exe. It doesn't really do that but I can live with it.

Posted on Friday, February 12, 2010 12:05 PM | Back to top


Comments on this post: Setting App.Config attributes at Run-time in C#

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © BelugaNeil | Powered by: GeeksWithBlogs.net