ini-parser icon indicating copy to clipboard operation
ini-parser copied to clipboard

Can not save commented INI

Open MMWPF opened this issue 8 years ago • 3 comments

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 ?

MMWPF avatar Jul 30 '17 05:07 MMWPF

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 avatar Jul 31 '17 08:07 rickyah

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

rickyah avatar Jul 31 '17 08:07 rickyah

#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.

Mrzhengsix avatar Aug 02 '22 17:08 Mrzhengsix