Although app. config is added in C # and access is convenient, sometimes it is not used to it. So let's do a kind of ini configuration file read and write under the C ++ imitation. Others write about calling the unmanaged kernel32.dll. I have also used it, but I feel that the compatibility is poor. Sometimes there may be encoding errors. After all, one is the previous system and the other is the current system. Let's write a configuration file read in the ini format of pure C #, which is actually a text file read/write. But what we need to do is not only to facilitate access operations, but also to future use.
We all know that the configuration items in the configuration file in ini format are separated by an equal sign for a line of text keys and values.
Like this:
Grade = 5.
Each configuration item is grouped together with the same type. It is called a section, which is separated by brackets.
Like this:
[Contact]
Qq = 1, 410910748
Website = assassinx.cnblogs.com
[Score]
Math = 85
Chinese = 90
Geographic = 60
The keys of each configuration item cannot be repeated in the section.
In this case, the concept of section is not very useful for convenience. In this way, we can understand all the configuration items as a dictionary structure to facilitate access and other operations. Why do we need to use dictionary? During the test, I found that the stored sequence of elements is not disrupted during the access process. We need to get the value based on the key. Note that we sometimes need to write comments in the configuration file. What should we do? Is the row starting with a semicolon. In this case, we can initialize a special key + serial number in the program, and make the same judgment when writing.
The whole process is:
When the program starts, it traverses all rows. If it starts with a semicolon (;), It stores this row as a configuration explanation. If not, it interprets this row and stores it in the dictionary set. It is easy to obtain the value based on the key during access. Note that the order of processing the comment row and changing the configuration to save it back to the row must be the same.
Let's get started:
Dictionary<, > Config( configData = Dictionary<,> fullFileName = Application.StartupPath + + hasCfgFile =File.Exists(Application.StartupPath + + (hasCfgFile == StreamWriter writer = StreamWriter(File.Create(Application.StartupPath + + StreamReader reader = StreamReader(Application.StartupPath + + indx = ((line = reader.ReadLine()) != (line.StartsWith() || configData.Add( + indx++ [] key_value = line.Split( (key_value.Length >= configData.Add(key_value[], key_value[ configData.Add( + indx++ ( (configData.Count <= ( key, configData[key] = StreamWriter writer = StreamWriter(fullFileName, IDictionaryEnumerator enu = (enu.Key.ToString().StartsWith( writer.WriteLine(enu.Key + + }
In this case, you don't need to test it, because I have been using it all the time.