Reading and writing to custom config in C #

Is there a lib in C # that will allow you to read and write to config or some kind of profile?

I'm not talking about app.config, but more like a custom.config file similar to ol.ini file where read / write functions are built in lib / functions.

I can of course write my own, but would like to know if there is some kind of common library or ways to do it ...

I am aware of the custom config section, but I prefer not to take this route.

Decision

I found what I was looking for. This will work:

http://www.codeproject.com/Articles/14465/Specify-a-Configuration-File-at-Runtime-for-aC-Co

+3


source to share


4 answers


I would use my config like class

and then use an XML serializer to read and write the config values.



public class MyConfig
{
  const string configPath = @"...";

  public string Setting1 { get; set; }
  ...

  public static MyConfig Load()
  {
     var serializer = new XmlSerializer(typeof(MyConfig));
     using (var reader = new StreamReader(configPath)
        return (MyCOnfig) serializer.Deserialize(reader);
  }

  public void Save()
  {
     var serializer = new XmlSerializer(typeof(MyConfig));
     using (var writer = new StreamWriter(configPath)
        serializer.Serialize(writer, this);
   }

      

+8


source


If you want really simple key-value pairs (like an old .ini or .cfg file), there's also local storage from ApplicationData.Current:

ApplicationData.Current.LocalSettings.Values["test"] = "Setting Value"; 
string t = (string)ApplicationData.Current.LocalSettings.Values["test"]; 

      



However, this is related to one application, but not sure how well it will handle the multi-thread / multi-instance problem.

0


source


Why are you just using the settings constructor (a Settings.settings

file in your project)?

0


source


If anyone needs to save the changes to a custom config file (in this case for a config file with the same name as the assembly dll):

ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = Assembly.GetExecutingAssembly().GetName().Name + ".dll.config";
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);

//... make modification (as you would for any app.config, etc...)

config.Save(ConfigurationSaveMode.Modified); 

      

0


source







All Articles