Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reload configuration settings from an external config file during run-time

I'm writing a game server in C# and would like to reload or refresh settings from a config file while the server is running.

Ideally I would like to save the settings in an XML file, have the ability to edit the file while the game server is running and then send the server the command to reload the settings from the file.

I know I can use a database to do this as well, but the game server is fairly small and I think it would be more practical to just save settings in a flat-file. I will have file-level access to the machine the server will run on.

What should I use?

like image 715
CarelZA Avatar asked Feb 08 '11 15:02

CarelZA


People also ask

How do I reload a config file?

To reload Config, select the loaded configurations you want to reload and click Reload. You can only reload a configuration that has the status of Loaded. To refresh Configs, click Refresh. Information for all of the configs in the table is redisplayed.

Can we change the configuration file at run time?

You can change and save these using the ConfigurationManager . You can modify and save user settings without restarting. You can't do that with application settings.

How do I save configuration settings file?

Right-click the Designer tab and click Save Config. File. The Saving Configuration to File dialog box appears. Navigate to the location in which you want to save the configuration file.

Which settings are stored in web config file?

A configuration file (web. config) is used to manage various settings that define a website. The settings are stored in XML files that are separate from your application code. In this way you can configure settings independently from your code.


2 Answers

Use http://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager.aspx

Use a Custom Configuration Section, hookup the sections from the app.config to external config file(s) by setting the location attrib of the section. All xml loading and serialization is done by those custom classes

Code provided by CarelZA:

First of all, ConfigurationManager caches the application's configuration by config section, and you can call ConfigurationManager.RefreshSection() to invalidate the cache for a specific section.

In app.config I added:

<configSections>
  <section name="gameSettings" 
           type="System.Configuration.NameValueSectionHandler,system , Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null"/>
</configSections>
<gameSettings configSource="game.config"/>

I created a file called "game.config" and set "Copy to Output Directory" to "Copy always".

In game.config:

<gameSettings>
  <add key="SettingName" value="SettingValue" />
</gameSettings>

Then in code, in order to access any setting:

settings = (NameValueCollection) ConfigurationManager.GetSection("gameSettings");
return settings["SettingName"];

And to reload the game config at any time when the reload command is sent to the server:

ConfigurationManager.RefreshSection("gameSettings");
like image 188
rene Avatar answered Sep 21 '22 18:09

rene


As per request posting my comment as an answer:

You can set it up so the server auto-loads the file settings with FileSystemWatcher. If you use a custom Settings class, you can simply lock the class, reload it from a file and unlock it (if you are using multiple threads).

Reading/writing from/to file or serialization is so trivial in .NET that that is probably not what you need help with and there are many options how to do it.

like image 34
Jaroslav Jandek Avatar answered Sep 19 '22 18:09

Jaroslav Jandek