Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SharpConfig (.ini files) example
#1
This code uses SharpConfig, which is widely used. There are two accessors for multiline settings at the end of the code. Note the line 5, beginning with 'string ScriptPD'. This is there for use in the Editor.

When compiling to an executable I 've been commenting out that line and changing
 
Code:
Copy      Help
var config = Configuration.LoadFromFile(scriptPD + @"\config.ini");
// ...
config.SaveToFile(scriptPD + @"\config.ini");
to
 
Code:
Copy      Help
var config = Configuration.LoadFromFile("config.ini");
// ...
config.SaveToFile("config.ini");
and put 'config.ini' in the same folder as the executable. I'm sure there's a better way, this seems like a kludge.

Code (attempting to paste per the instructions rather than using the 'Code' button:
 
Code:
Copy      Help
/*/ nuget Config\SharpConfig; /*/
using SharpConfig;

// Create string 'scriptPD', representing the parent directory of the running script.
string scriptPD = pathname.normalize(@"%folders.Workspace%\Files" + (script.path + @"\..\"));
// Read the configuration file
var config = Configuration.LoadFromFile(scriptPD + @"\config.ini");

// Get the value of a setting with multiline text
string multilineValue = GetMultilineValue(config["Section"]["MultilineSetting"].StringValue);

// Print the multiline value
print.it(multilineValue);

// Update the multiline value with an actual multiline string
string newMultilineValue = @"This is a multiline string.
It contains multiple lines.
Each line is separated by a line break.

There should be a blank line above this if you print it."
;

config["Section"]["MultilineSetting"].StringValue = SetMultilineValue(newMultilineValue);

// Save the configuration file
config.SaveToFile(scriptPD + @"\config.ini");


static string GetMultilineValue(string value) {
    // Deserialize the multiline string using a delimiter
    return value.Replace("||", Environment.NewLine);
}


static string SetMultilineValue(string value) {
    // Serialize the multiline string using a delimiter
    return value.Replace(Environment.NewLine, "||");
}
#2
Thanks a lot,
I was looking for this.
#3
You bet, hope it works for you.


Forum Jump:


Users browsing this thread: 2 Guest(s)