Using NINI with IsolatedStorage
-
19-06-2021 - |
题
I'm trying to save some configuration info and I wish to use NINI library and files within IsolatedStorage. I don't know why but when I try to save the configuration I got the following exception on Nini Save method: Source cannot be saved in this state
Here's the code:
isf = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly | IsolatedStorageScope.Domain, null, null);
var isfs = new IsolatedStorageFileStream("file.ini", FileMode.Create, isf);
//Stream s = new StreamWriter(isfs);
IConfigSource mainSource = new IniConfigSource(isfs);
var config1 = mainSource.Configs["General"];
if (config1 == null)
{
mainSource.AddConfig("General");
config1 = mainSource.Configs["General"];
}
config1.Set("CW", CommentWidth);
mainSource.Save();
isfs.Close();
The exception happens in mainSource.Save(); . Any ideas?
解决方案
From the constructor documentation for IniConfigSource(Stream)
:
This is a non-savable source unless you call one of the overloaded Save methods.
So, you probably want Save(Stream)
. You might want to Seek the stream back to the origin before passing it to Save
.
Contrary to your comments, I tried this out, and discovered that the isfs
stream had been closed (presumably by the constructor method - it's not documented what it will do to the stream) - so I got a different error message. This led me to re-initializing isfs
and, as I had said, passing it to the Save
method:
isfs = new IsolatedStorageFileStream("file.ini", FileMode.Create, isf);
((IniConfigSource)mainSource).Save(isfs);
and it works fine.