Maintaining multiple settings files - c#

Currently, I have a form that has various radio buttons, directory browsers, date pickers etc. The application uses the settings, and executes a file deletion task. I was wondering what is the best strategy to save these settings to an external file that can be loaded at a later date. So essentially each configuration can be loaded, executed, and then another configuration loaded. Also, the configuration can be passed across installations / users.
I dont believe I can use the internal .settings file because I will have multiple sets of settings that can be loaded.

There are many formats to choose from for storing settings such as:
INI
CSV
YAML
JSON
XML
One of the best strategies would be to use XML. XML is one of the most popular formats for setting/configuration files because its easy to parse, the syntax is well specified and is powerful yet easy to read.
Using XML files allows configurations to be passed across installations and shared among users.
In addition if you have an XSD (schema file) that describes your XML configuration files, it is trivial for your application to validate different configuration files.

Related

How to add new sections to app config file in C#

I'm using Virtual Studio 2017 to develop a WPF application. I want my application to get a table of data from server and to save the table to the app configuration file. Each row of the table will be a section in the app configuration file. The problem is I cannot find any way to save data like that.
Example: We can use GetSection method to get a setting section but there seems to be not any method like AddSection or SetSection to add a new section.
I wonder if WPF supports this.
You have various options. It's not obvious which framework version you are using. Since .NET Core, there is no app.config file anymore (by default). It's replaced with .settings files.
app.config
You can modify the .config file directly (XML) - only recommended to add elements like sections
Use the ConfigurationManager class (example) to read/write data and create sections
Use XmlDocument (or any other technique to handle XML data) to handle the XML content directly
The XML handling is quite complex, even when using the ConfigurationManager. There are better solutions, like JSON files, that are far more convenient.
.settings
Use the Visual Studio Settings Designer GUI
use the API of the ApplicationSettingsBase class (example)
Edit the code-behind file manually
As the name .settings implies the intention of those files is to store settings and not application data. Of course you can use the .settings file to store any data, but then make sure to create a dedicated file for such data (by adding a new item to your project) so that you don't mix settings with data.
Since .settings is property based (key-value-pair), storing data sets like tables or other data structures (e.g. tree) is not very convenient.
The common way to store application data is to serialize C# objects to JSON text objects (JSON serialization and deserialization) or binary format (Serialization (C#)), simply write data as structured plain text file like .csv files, XML and when speaking of tables, of course, we instantly think of a database (e.g. simple file based db like SQLite).

Design implementation : Uploading XML file and evaluation of predefined path

I have a .xml file, which is the result of an export from a non-relational database.
In my application, as a configuration, a user should be able to select nodes from a xml structure, which would be compared/evaluated later on when he upload the .xml.
I have thought to 2 options, but am not very pleased with both:
option1: the structure of the .xml file has to be stored in the database, so the application can display a TreeView, and the user would simply select the nodes to inspect.
(cons: I have many .xml files, one for each system it relates too and each .xml is quit large)
option2: the user would have to upload a .xml file before starting his configuration, so that its structure would be dynamically generated.
(cons: User has one more step to do in order to make his configuration. The one .xml file that he uploads may not contains all the nodes it could have)
Or maybe there are different ways than displaying a TreeView for that purpose ? As it is hard for me to think out of the box, I can't see other options.
I hope this is clear enough,
Maybe there is a kind of best practices I have missed, I am open to suggestions.
I would go with an intermediate option.
Generate dynamically the xsd schemes corresponding to your xml files and store them somewhere.
Process all the xml files in order to have the most complete xsd, because you said that each xml may not contain the whole structure.

Save different data settings with C# (variables) not as plain text easy in a specific filepath and access easy

How do I save my data...
(compressed/encrypted) not human readable in a portable file by
influencing on directory name and file name with using easily
addressable settings by "name" and "value" like in registry/ini
with the possibility to access the same one settings file (machine based)
with any executable
In VB I did that with INI Files, but now I have heard from MS that the Framework offers no function to access INI Files anymore. Since INI Files are also not any longer up-to-date for using them in a new application I wanted something similar. Just to write to a text file line by line is not what I want to do, I will explain why: If I will need a setting named "Label4" I have to read the entire file and search for the line containing it and then I have even to split the result in setting name and setting value (.NET has another syntax split function).
Let's say I have a thousand labels and a hundred textboxes in a form and wanna save its content easily to a file or whatever and read it the same easily back addressing the settings by a name already how would I do that? I am ready to use a DB but don't know how to setup and save information there. Easy would be to access the registry with "name" and "value", but I prefer a portable file.
The data shall be stored compressed or encrypted not in a plain text human readable way (I mean XML / html is not what I search for to store the data else I could research again how to use INI files with C# which might be possible I guess). I stumbled upon "application settings" but this doesn't save my data compressed nor can I simply influence on path and filename of the settings file. With each new EXE all the stored settings are lost again for those new EXE if I have used "application settings". I need not "user/application based" settings but "machine based", I hope you understand what I wanna say. Any App on my comp shall be able to get to my settings file
and I shall be able to choose the place/filename where to save the settings.
You should write to any xml file and get your desired results.
(compressed/encrypted) not human readable in a portable file
You can both encrypt/compress a normal file
influencing on directory name and file name with using easily
Can't understand what you mean
addressable settings by "name" and "value" like in registry/ini
xml gives you flexibility you need
with the possibility to access the same one settings file (machine based)
You can keep it machine based.
with any executable
What does it mean?

Storing a blob in application settings

What is the appropriate method of storing a blob (a large string of bytes) in (or with) application settings in .NET?
I can think of several approaches, but none that seem as simple as it should be.
Storing a base64 or hex string
Slightly unwieldy for serialization/deserialization
Storing a file beside the user.config (or app.config) file and managing it manually
I don't know how to locate the user.config file programmatically
Storing a file elsewhere in AppData and managing it manually
Prevents my application data from being in one spot
I need to be able to change the value at runtime, and have distinct values for each user, because this data will have tight ties to what is in user.config.
What is the ideal method for storing such a value?
In a Visual Studio project you can store files in a Resource file. The resource file can store strings and files. This is by far the most convenient solution and no serialization required.
Victor

RESX files and xml data

What is the best way to store XML data used in a program ? Use RESX file or store it as a .xml file and load and unload the files as per requirement
A third option would be to put the XML file as embedded resource in the assembly. In that case, use Assembly.GetManifestResourceStream() to load the XML.
As Cerebrus wrote, when localization is necessary, RESX would be the way to go.
.resx files are XML files albeit conforming to a particular schema (Microsoft ResX Schema v2.0). This schema was designed with the explicit aim of being easily human readable and editable manually.
I see no problem with storing your data as XML files. Basically it depends on the function of the data - If it is localizable resources that you are trying to store, go with the established .resx files. If not, you are free to use your XML with custom schema.
Putting XML (blobs) in .resx
Positives:
Readily available
Negatives:
It would increase the memory load, if the content is big in size. Big-in-size is in comparison to assembly size without content. If an assembly size is 10kb and content size is 10kb, even though 10kb is not bigger in today's scenario, one can reduce assembly size to half just by keeping content in separate file.
Code manageability goes to down drastically, by keeping XML as string Key-value-pairs in
RESX. As resx editor is not be XML sensitive.
If one is very keen keeping xml content as embedded resource, you can create XML file and keep it as file resource inside RESX.

Categories

Resources