rickyah / ini-parser

Read/Write an INI file the easy way!
MIT License
971 stars 241 forks source link

Can not save commented INI #152

Open MMWPF opened 7 years ago

MMWPF commented 7 years ago

I am reading an ini file, which contains inline comments. I am changing it a bit (Add keys, Update keys values...) and then save it. In the outup ini I have 2 major chnages:

  1. The comments does not keep
  2. The assigment changed from Key=Value to Key = Value (Wrapping spaces)

Example file (Before) [Example] ABC=2 ;This is a very importend comment DEF=3 ;Yet, another very importend comment

Example file (After) [Example] ABC = 2 DEF = 3

Here is the code I am using: string exampleFilePath = "TBD add example file path here"; FileIniDataParser fileIniData = new FileIniDataParser(); fileIniData.Parser.Configuration.CommentRegex = new Regex(";(.)"); IniData exampleFileData= fileIniData.ReadFile(exampleFilePath); KeyDataCollection generalSection = exampleFileData.Sections["Example"]; / In this section I am changing things in the file */ fileIniData.WriteFile(exampleFilePath, exampleFileData);

Any ideas ?

rickyah commented 7 years ago

Yep, I'm afraid that's a flaw in the library: it does not saves where the comments, or any other construct, for the matter, are located in relation with the rest of the file. In other words, this library does not takes into account the physical layout of elements on the file.

I'm very aware of the issue, and several people show concern about the problem, so it is something I want to solve for version 3.0. I'm getting holidays very soon and hopefully I can devote some time to work on in.

Sorry for the inconvenience

rickyah commented 7 years ago

Sorry, missed your question about the wrapping spaces in key/value pairs, you can change the character to use as spacer at IniData.Configuration.AssigmentSpacer = '';

https://github.com/rickyah/ini-parser/blob/development/src/IniFileParser/Model/Configuration/IniParserConfiguration.cs#L199

Mrzhengsix commented 2 years ago

2022.8.3

To MMWPF Guys,I think maybe we can use this to fix your problem. fileIniData.Parser.Configuration.CommentRegex = new Regex(@";[\s\S]*$"); The only regret is that the comment is above the key,but i think that would be okay than the problem.