public class Settings
{
public static readonly string fileName = "config.ini";
private IConfigSource src
{
get
{
CreateIfNotExists();
return new IniConfigSource(fileName);
}
}
public void test1()
{
//var src = new IniConfigSource(fileName); ;
src.Configs["DATA"].Set("baa", "haaaaee");
src.Save();
}
public void test2()
{
var src2 = new IniConfigSource(fileName); ;
src2.Configs["DATA"].Set("baa", "haaaaee");
src2.Save();
}
public Stream CreateIfNotExists()
{
if (!File.Exists(fileName))
{
Stream file = File.Create(fileName);
return file;
}
return null;
}
}
why the test2() method works fine and test1() not works?
It doesn’t work because your “src” property creates a new IniConfigSource every single time it’s called. Your “test” is expecting it to be the same one across calls.
Store the result of your IniConfigSource in a private variable, and use that if it’s set.
I’m not going to get into why CreateIfNotExists is there at all, considering the Stream returned is never used and discarded to the GC.