闽公网安备 35020302035485号

Configuration.LoadFromFile("myConfig.cfg"); // 文件
Configuration.LoadFromStream(myStream); // 文本流
Configuration.LoadFromString(myString); // 文本
Configuration.LoadFromBinaryFile("myConfig.cfg"); // 二进制
Configuration.LoadFromBinaryStream(myStream); // 二进制流
文件保存myConfig.SaveToFile("myConfig.cfg"); // 文件
myConfig.SaveToStream(myStream); // 文件流
myConfig.SaveToBinaryFile("myConfig.cfg"); // 二进制文件
myConfig.SaveToBinaryStream(myStream); // 二进制流
使用方法var cfg = new Configuration();
cfg["SomeStructure"]["SomeString"].StringValue = "foobar";
cfg["SomeStructure"]["SomeInt"].IntValue = 2000;
cfg["SomeStructure"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 };
cfg["SomeStructure"]["SomeDate"].DateTimeValue = DateTime.Now;
cfg.SaveToFile(filename);
对象操作var cfg = new Configuration();
//对象. 堆代码 www.duidaima.com
var p = new SomeClass
{
SomeString = "foobar",
SomeInt = 2000,
SomeInts = new[] { 1, 2, 3 },
SomeDate = DateTime.Now
};
//设置
cfg.Add(Section.FromObject("SomeStructure", p));
数组操作
var cfg = new Configuration();cfg["GeneralSection"]["SomeInts"].IntValueArray = new[] { 1, 2, 3 };
// 获取数组类型值
int[] someIntValuesBack = cfg["GeneralSection"]["SomeInts"].GetValueArray<int>();
float[] sameValuesButFloats = cfg["GeneralSection"]["SomeInts"].GetValueArray<float>();
string[] sameValuesButStrings = cfg["GeneralSection"]["SomeInts"].GetValueArray<string>();
// 获取数组对象
object[] sameValuesButObjects = cfg["GeneralSection"]["SomeInts"].GetValueArray(typeof(int));
配置文件注释//获取包含所有有效注释分隔字符的数组。当前值为{“#”,“;”}。 Configuration.ValidCommentChars{get;} //获取或设置保存配置时的首选注释字符。默认值为“#”。 Configuration.PreferredCommentChar{get;set;} //获取或设置设置的数组元素分隔符。默认值为“,”。 Configuration.ArrayElementSeparator{get;set;} //获取或设置一个值,该值指示在分析配置时是否应忽略内联注释。 bool Configuration.IgnoreInlineComments{get;set;} //获取或设置一个值,该值指示在分析配置时是否应忽略前置注释。 bool Configuration.IgnorePreComments{get;set;} //获取或设置一个值,该值指示在创建配置时是否应添加等号之间的空格。 bool Configuration.SpaceBetweenEquals{get;set;} //获取或设置一个值,该值指示字符串值是否不带引号,但包括其间的所有内容 bool Configuration.OutputRawStringValues{get;set;}
项目地址:
https://github.com/cemdervis/sharpconfig