public void SaveInfoState(InfoState state) { // Temperary use lib's log folder var path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\NvApiHelper"; if (Directory.Exists(path) == false) { Directory.CreateDirectory(path); } var fileName = state.DisplayName.Replace(' ', '_') + ".json"; var ser = new DataContractJsonSerializer(typeof(InfoState)); var stream = new MemoryStream(); ser.WriteObject(stream, state); //show stream json output stream.Position = 0; var json = ""; using (var sr = new StreamReader(stream)) json = sr.ReadToEnd(); Console.WriteLine(json); using (var sw = new StreamWriter(path + "\\" + fileName, false, Encoding.UTF8)) { sw.WriteLine(json); } stream.Dispose(); }
public InfoState LoadInfoState(string displayName) { // Temperary use lib's log folder var path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\NvApiHelper"; var fileName = displayName.Replace(' ', '_') + ".json"; if (File.Exists(path + "\\" + fileName) == false) { return(null); } InfoState result = null; using (var fs = new FileStream(path + "\\" + fileName, FileMode.Open, FileAccess.Read)) { MemoryStream stream = new MemoryStream(); fs.CopyTo(stream); stream.Position = 0; var ser = new DataContractJsonSerializer(typeof(InfoState)); result = (InfoState)ser.ReadObject(stream); stream.Dispose(); } return(result); }