Best way to save low amount of configuration

2019-08-25 04:26发布

问题:

I have to save the configuration of my device, and a friend told me that as it is just 2(two) fields, it would be better save/read it on a .txt file instead create a table. Is that true ?
Will it be quite faster? Or doesn't matter ?
If so, how may I do that?
I know how to Write/Read on text file using System.IO and StreamReader/StreeamWriter.
I just wanted to know if it is true that it would be faster and an idea of how could I read it and set the value read to the configuration.... Maybe using array and use Split ?

回答1:

It shouldn't really matter from a performance perspective, so don't roll your own just use the built in C# properties/app.config implementation.



回答2:

Just to expand on the answer that Yaur presented.

Using the built in configuration settings would be preferred as it will be faster (performance would be fairly negligible though) and simpler then storing in a table (I am assuming you are talking about a database table) or using your own configuration.

Using the built in configuration is fairly simple and there are plenty of examples online if you are not sure how its done, so I am not going to provide an example of that.

Depending on the information and how you wish to consume it, it may be more beneficial to store to the database as the configuration will be persisted even if you reinstall your application (assuming the database is on a server and not a part of your application - sqlce)

One last thing to keep in mind is where will you be using your configuration settings. The built in configuration settings do not work with application libraries (dlls). Because of this if the configuration settings will be for a library you will have to create your own.

The simplest workaround for this is to use ConfigurationManager. Create a .config (xml) file under your project and name it same as your dll (ie AttachmentProcessor.dll.config). This is where you would put your settings with a key/value pair. Make sure you set the file property "Copy to Ouput Directory" to "Copy Always" or "Copy if Newer".

Example of a config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>    
    <add key="AttachmentDirectory" value="C:\TestDir"/>
    <add key="FolderSlug" value="TEST"/>
  </appSettings>
</configuration>

Then in your code in the constructor you would read the config file:

var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
attachmentDirectory = appConfig.AppSettings.Settings["AttachmentDirectory"].Value;
folderSlug = appConfig.AppSettings.Settings["FolderSlug"].Value;

Then you would use the settings where needed.

Hope this helped!



回答3:

Using a Table would be an overkill in your case. Just think that using a file you could say that directly would be dealing with the File System, while with a table, you would be dealing with a Database Engine that then would be dealing with the File System (this is a big simplification).

You could use a txt file and then define whatever format you may want. You could have a different setting in each line and then parse it as you would like. If you use a txt file is really up to you how you parse the information.

As Yaur suggested it seems in your case it would better to use the app.config file of your application.

Here you have an example of how access your settings with the Configuration Manager



回答4:

Using the default settings is just fine for this case and all you have to do is to set the Scope of the setting to User. The you can update it and save it:

Properties.Settings.Default.Writable = "new value";
Properties.Settings.Default.Save();