我正在编写一个Windows Phone Silverlight应用程序。我想将一个对象保存到JSON文件中。我已经编写了以下代码。
string jsonFile = JsonConvert.SerializeObject(usr);
IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("users.json", FileMode.Create, isoStore);
StreamWriter str = new StreamWriter(isoStream);
str.Write(jsonFile);
这已经足以创建一个JSON文件,但是它是空的。我做错了什么吗?难道不应该将对象写入文件中吗?
StreamWriter
?或者更好的方式是使用using (var str = new StreamWriter(isoStream)) { str.Write(jsonFile); }
语句来包装它?请参阅“如何正确使用 StreamWriter 类?”(链接:https://dev59.com/Omgu5IYBdhLWcg3wP01m) - dbcStreamWriter
,则您写入的某些内容可能无法刷新到磁盘。 - dbc