I have several sections on my App.config file, saved as DictionarySectionHandlers. I can easily read them by casting them to a hashtable. Now, what I want, is to also be able to save a hashtable/dictionary as a DictionarySectionHandler, back to the App.config file. How is that possible?
Related
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).
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?
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
I would like to embed a .txt file into my C# project storing a list values for the user. These values should be configurable and therefore the .txt will have to be edited during runtime. I have found out that Embedded Resources cannot be modified. Is there any other way to do that?
Thank you.
Store the text file as an embedded resource. The first time your program is run, copy the embedded resource to a file on disk, and use it for the configuration. Your users can edit the disk file.
The embedded resource version serves as a default configuration.
You can use your app.config or web.config configuration files.
Normall if you will use large amout of data using a database is recommended. I assume you really need just a .txt document. In your assembly write a procedure that will create that text file if its not present. To be more specific lets say your program is mainProgram.exe. In onload event of mainprogram.exe write a procedure checkTxtFile(). This procedure first will check if there is a txt file in the directory.If the file is not presend it will create it with the desired values.
You can create a XML file using Filestream and using it during runtime or
make your own protocol format and store the data in the file so that no one can change it.
Over to that you can also encrypt and decrypt the file on the fly.
Lists of modifyable, persistable values are best stored in a database. There are many lightweight options to choose from; in this case I think a SqlLite database would suit your purposes best.
I have web.config file from some application. It is located in some random location. I have to parse this web.config file (get all keys names and values). I tried to use ConfigurationManager class in order to get those data however, it throws exception when I try to get some Sections (Configuration->GetSection('section name')). It throws exception because I do not have dll that this section points to (because I have only web.config not whole application). It seems that GetSection method check underlying dll in order to get more info, but I just need value (name of dll).
What can I do, to turn off this mechanism, do you know other simple solutions to get it done ?
You are just going to have to use XmlDocument or XDocument (3.5) to parse the file.
If you just want to read the text, and not do any web.config-specific processing, use the fact that a .config file is XML, and use your favourite usual way of reading and parsing XML.
Web.Config files are just XML and an be read using a number of .Net XML objects. Below are a couple of methods.
Tutorial on reading an XML file using XmlTextReader http://support.microsoft.com/kb/307548
Tutorial on reading an XML file using LinqToSQL http://www.mssqltips.com/tip.asp?tip=1524