Comments (4)
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.
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.
For the example code you give on your blog I get: System.Collections.Generic.KeyNotFoundException: 'FavouriteColor'
from peanutbutter.
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:
- What are ApplicationSettings
I'm assuming that you're reading fromappSettings
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:
-
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.
-
app settings are in xml -- which is often more difficult for people to deal with than INI format, but not impossible, of course
-
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")
-
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. -
So why use PeanutButter.INI?
- 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)
- If you'd like to easily load up different configuration files at runtime
-
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)
- Add Option (or default) for Section Gaps HOT 3
- Adding some README and documentations to explain INI HOT 1
- Servicepath can not be set // any documentation? HOT 3
- Feature: maintain order of sections and settings within file
- Quotes HOT 2
- CORS policy HOT 3
- semi-colon ini text value causes truncation HOT 21
- IniFile should not use ordinal Dictionary HOT 4
- ini Remove method leaves the key behind HOT 3
- Per-project documentation
- Switch build, pack & push to use `dotnet` HOT 1
- Project restructuring to simplify dev, especially multi-platform dev
- semi-colon ini text value causes truncation (2022) HOT 9
- SetValue return value HOT 1
- How to set encoding? HOT 1
- PeanutButter.INI: CommentDelimiter not respected when saving HOT 2
- PeanutButter.INI exception HOT 6
- [PeanutButter.INI] How can I add an empty line at the last line? HOT 6
- PeanutButter.DuckTyping: Parameter count error when trying to invoke overloaded method HOT 7
- Update PeanutButter.EasyArgs to be AOT-friendly
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from peanutbutter.