Reading and Writing Configuration Files

Reading and writing values in.NET .config files

I was recently tasked with a similar problem, I had to change the location of where settings files were read from the default location in AppData to the Application directory. My solution was to create my own settings files that derived from ApplicationSettingsBase which specified a custom SettingsProvider. While the solution felt like overkill at first, I've found it to be more flexible and maintainable than I had anticipated.

Update:

Sample Settings File:

public class BaseSettings : ApplicationSettingsBase
{
protected BaseSettings(string settingsKey)
{ SettingsKey = settingsKey.ToLower(); }


public override void Upgrade()
{
if (!UpgradeRequired)
return;
base.Upgrade();
UpgradeRequired = false;
Save();
}


[SettingsProvider(typeof(MySettingsProvider)), UserScopedSetting]
[DefaultSettingValue("True")]
public bool UpgradeRequired
{
get { return (bool)this["UpgradeRequired"]; }
set { this["UpgradeRequired"] = value; }
}
}

Sample SettingsProvider:

public sealed class MySettingsProvider : SettingsProvider
{
public override string ApplicationName { get { return Application.ProductName; } set { } }
public override string Name { get { return "MySettingsProvider"; } }


public override void Initialize(string name, NameValueCollection col)
{ base.Initialize(ApplicationName, col); }


public override void SetPropertyValues(SettingsContext context, SettingsPropertyValueCollection propertyValues)
{
// Use an XmlWriter to write settings to file. Iterate PropertyValueCollection and use the SerializedValue member
}


public override SettingsPropertyValueCollection GetPropertyValues(SettingsContext context, SettingsPropertyCollection props)
{
// Read values from settings file into a PropertyValuesCollection and return it
}


static MySettingsProvider()
{
appSettingsPath_ = Path.Combine(new FileInfo(Application.ExecutablePath).DirectoryName, settingsFileName_);

settingsXml_ = new XmlDocument();
try { settingsXml_.Load(appSettingsPath_); }
catch (XmlException) { CreateXmlFile_(settingsXml_); } //Invalid settings file
catch (FileNotFoundException) { CreateXmlFile_(settingsXml_); } // Missing settings file
}
}

bash reading and writing a config file with an unusual layout

For the first question, pre-process your .cfg file and then read it, like this:

CONFIG_FILE=test.cfg
sed 's/ = /=/g' < test.cfg > /tmp/processed.cfg
. /tmp/processed.cfg

Now, all your name_1, name_2 pairs will be available to your shell scripts.

For the second question, do it like this:

CONFIG_FILE=test.cfg
TARGET_KEY='name_1 '
REPLACEMENT_VALUE='"true" '
sed -i -e "s/\($TARGET_KEY *= *\).*/\1$REPLACEMENT_VALUE/" $CONFIG_FILE

How to read write from the exe-files configuration file?

This isn't how settings works in winforms. Call up the properties for your project (right click the project, choose properties). Click settings. Enter your settings in there - for example make one called TimerInterval, of type int, user scope, value set to 3000

To use them from code do:

_myTimer.Interval = Properties.Settings.Default.TimerInterval;

To change settings, set like any other property:

Properties.Settings.Default.TimerInterval = 2000;

To save them do:

Properties.Settings.Default.Save();

Only user scoped settings can be altered/saved at runtime. Application scopes settings are altered by changing the Settings grid as above. It is intended you use user scopes settings for things the user will change /prefs and app scoped for things your app needs to run but you as developer want to configure (db connection string perhaps)

PHP Config File Read and Write

If you want to do so, you need to store you configuration in a non-PHP file.

You can, for example, store your config in an .ini file and read it with parse_ini_file().



Related Topics



Leave a reply



Submit