Git Product home page Git Product logo

Comments (4)

Flakie99 avatar Flakie99 commented on August 21, 2024 1

Wow, thanks very much for this. It will be some interesting reading. Hopefully I can get it all working.
I have just setup a new Settings Provider and am testing it now. All seems to be working well and the xml is now saving in the application folder. So all good as I need this app to be portable when finished
But I still want to implement this with ini files as i need the multiple configs as well as portable.

Edit: Just had a thought. Maybe I wont even need the new Settings Provider if I can get the ini working.

from peanutbutter.

Flakie99 avatar Flakie99 commented on August 21, 2024 1

Thanks very much for the demo. I have had a quick look at it now and it is amazing. Much better than I was hoping for, I am sure I will be able to use this in my application. I will not know the amount of ini files I will be using. Potentially, in the future, it could be hundreds and the name will change a lot for each one (it will be based on a persons name and the device type they are using). I am sure I can code something now. Fantastic Thanks again.

from peanutbutter.

Flakie99 avatar Flakie99 commented on August 21, 2024

For the example code you give on your blog I get: System.Collections.Generic.KeyNotFoundException: 'FavouriteColor'

from peanutbutter.

fluffynuts avatar fluffynuts commented on August 21, 2024

Hi

Thanks for submitting an issue (:

First off, here's a demo I cracked together for using PeanutButter.INI from a WinForms app: https://github.com/fluffynuts/PeanutButter.INI.WinForms.Demo

Hopefully it answers your questions about how to use PeanutButter.INI

To answer your other questions here:

  1. What are ApplicationSettings
    I'm assuming that you're reading from appSettings in your app.config (which is output alongside your .exe as whatever-your-program-is-called.exe.config), and that you're probably using ConfigurationManager, like the MSDN docs

In this case, you're reading from (and eventually writing to) an xml file, and only dealing with a very specific part of that xml file

There are pros and cons with different forms of configuration (as there are with all things), but in this case, if we are to compare app.config (xml) vs INI files:

  1. You only have one app.config for your application. You can't (easily) swap them out at runtime -- the infrastructure is just not designed for it.

  2. app settings are in xml -- which is often more difficult for people to deal with than INI format, but not impossible, of course

  3. there's no concept of "sections" within app settings, where INI files have this concept baked in -- you can get around this in app settings, of course, (eg name your settings like "Section1.Setting1")

  4. Does PeanutButter.INI read from / write to application settings?
    No. As you've probably figured out from above, the files are in totally different formats.

  5. So why use PeanutButter.INI?

    1. If you prefer the INI format for settings (as I say, quite a few people find INI easier to read and write by hand than xml)
    2. If you'd like to easily load up different configuration files at runtime
  6. What does this exception mean: System.Collections.Generic.KeyNotFoundException: 'FavouriteColor'
    Ok, so PeanutButter.INI abstracts how INI files work by presenting the developer with a dictionary of dictionaries.

So, since you've said you're new to this, a quick rundown of dictionaries:
A dictionary in .NET is a collection of key/value pairs which are quick to read from, even if there are a lot of keys. Keys would be equivalent to "setting names" and values equivalent to "setting values" for your use-case

.NET Dictionaries are found in the System.Collections.Generic namespace, because they are generic collections: you can create a dictionary and tell it what type the keys and values will be -- they can be anything, but they will be consistent for all key/value pairs in the collection.

PeanutButter uses Dictionary<string, string>, so keys are strings, and so are the values. Looking closer, IINIFile presents a top-level dictionary of sections, so if you load the file:

[general]
foo=bar

you can reference ini["general"] and you'll get a dictionary containing one key/value pair: "foo" => "bar". You can "reach directly into" the second dictionary, if you know for sure that the setting exists:

var fooValue = ini["general"]["foo"];

However, if you asked for:

var anotherValue = ini["general"]["another"];

you'd get a KeyNotFoundException, because the general section doesn't have a setting called another. You can ask IINIFile if a setting exists though:

if (ini.SettingExists("general", "another"))
{
	Console.WriteLine($"general.another is: {ini["general"]["another"]}");
}
else
{
	Console.WriteLine("no 'another' setting found in section 'general');
}

PeanutButter.INI also makes section generation transparent, so you can just directly assign to a value in a section which doesn't (yet) exist, and that's ok:

ini["new-section"]["setting"] = "hello";

Anyways, check out the demo -- I hope it helps (:

from peanutbutter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.