private static void TimeTracker_LoadAccounts() { dctAccounts = new Dictionary <string, TimeTrackerAuthentication>(); System.IO.IsolatedStorage.IsolatedStorageFile isoStore; isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(System.IO.IsolatedStorage.IsolatedStorageScope.User | System.IO.IsolatedStorage.IsolatedStorageScope.Assembly, null, null); System.IO.IsolatedStorage.IsolatedStorageFileStream fsSFS; if (!isoStore.FileExists("settings.txt")) { return; } fsSFS = new System.IO.IsolatedStorage.IsolatedStorageFileStream("settings.txt", System.IO.FileMode.Open, isoStore); System.IO.StreamReader srSFS = new System.IO.StreamReader(fsSFS); String strFile = srSFS.ReadToEnd(); String[] arrLines = strFile.Split(new char[] { '\r', '\n' }); foreach (String strLine in arrLines) { if (strLine.Trim().Length == 0) { continue; } String[] arrAccount = strLine.Split('|'); TimeTrackerAuthentication auth = new TimeTrackerAuthentication(); auth.EmailAddress = arrAccount[1]; auth.Token = arrAccount[2]; dctAccounts.Add(arrAccount[0], auth); } srSFS.Close(); srSFS.Dispose(); srSFS = null; fsSFS.Close(); fsSFS.Dispose(); fsSFS = null; }
public static void Serialize(object obj, string nameFile) { var keyLock = nameFile.ToLower(); try { var nTry = 0; var nTryError = 0; do { nTry = nTryError; var stream = new System.IO.IsolatedStorage.IsolatedStorageFileStream(nameFile, System.IO.FileMode.Create, System.IO.FileAccess.Write); try { var xml = new System.Xml.Serialization.XmlSerializer(obj.GetType()); xml.Serialize(stream, obj); } catch (Exception ex) { System.Diagnostics.Debug.Print(ex.Message); System.Diagnostics.Debugger.Break(); nTryError += 1; System.Threading.Tasks.Task.Delay(500); } finally { stream?.Dispose(); } } while (!(nTry == nTryError || nTryError > (5000 / 500))); } catch (Exception ex) { throw ex; } }
public static object Deserialize(string NameFile, Type Type = null) { object Obj = null; if (System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication().FileExists(NameFile)) { ; } { string KeyLock = NameFile.ToLower(); Exception Er = default(Exception); try { int NTry = 0; int NTryError = 0; do { NTry = NTryError; System.IO.Stream Stream = new System.IO.IsolatedStorage.IsolatedStorageFileStream(NameFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Inheritable); try { System.Xml.Serialization.XmlSerializer XML = new System.Xml.Serialization.XmlSerializer(Type); Obj = XML.Deserialize(Stream); Er = null; } catch (Exception ex) { Er = ex; NTryError += 1; System.Threading.Tasks.Task.Delay(500); } finally { if (Stream != null) { Stream.Dispose(); } } } while (!(NTry == NTryError || NTryError > (5000 / 500))); } catch (Exception ex) { throw ex; } } return(Obj); }
public static object Deserialize(string nameFile, Type type = null) { if (!System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication().FileExists(nameFile)) { return(null); } object obj = null; var keyLock = nameFile.ToLower(); var er = default(Exception); try { var nTry = 0; var nTryError = 0; do { nTry = nTryError; System.IO.Stream stream = new System.IO.IsolatedStorage.IsolatedStorageFileStream(nameFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Inheritable); try { var xml = new System.Xml.Serialization.XmlSerializer(type); obj = xml.Deserialize(stream); er = null; } catch (Exception ex) { er = ex; nTryError += 1; System.Threading.Tasks.Task.Delay(500); } finally { stream?.Dispose(); } } while (!(nTry == nTryError || nTryError > (5000 / 500))); } catch (Exception ex) { throw ex; } return(obj); }
private static void TimeTracker_SaveAccounts() { System.IO.IsolatedStorage.IsolatedStorageFile isoStore; isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(System.IO.IsolatedStorage.IsolatedStorageScope.User | System.IO.IsolatedStorage.IsolatedStorageScope.Assembly, null, null); System.IO.IsolatedStorage.IsolatedStorageFileStream fsSFS; fsSFS = new System.IO.IsolatedStorage.IsolatedStorageFileStream("settings.txt", System.IO.FileMode.Create, isoStore); System.IO.StreamWriter swSFS = new System.IO.StreamWriter(fsSFS); foreach (String strAccount in dctAccounts.Keys) { TimeTrackerAuthentication auth; auth = dctAccounts[strAccount]; swSFS.WriteLine(strAccount + "|" + auth.EmailAddress + "|" + auth.Token); } swSFS.Flush(); swSFS.Close(); swSFS.Dispose(); swSFS = null; fsSFS.Close(); fsSFS.Dispose(); fsSFS = null; }
public static void Serialize(object Obj, string NameFile) { string KeyLock = NameFile.ToLower(); Exception Er = null; try { int NTry = 0; int NTryError = 0; do { NTry = NTryError; var Stream = new System.IO.IsolatedStorage.IsolatedStorageFileStream(NameFile, System.IO.FileMode.Create, System.IO.FileAccess.Write); try { System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(Obj.GetType()); xml.Serialize(Stream, Obj); } catch (Exception ex) { NTryError += 1; System.Threading.Tasks.Task.Delay(500); } finally { if (Stream != null) { Stream.Dispose(); } } } while (!(NTry == NTryError || NTryError > (5000 / 500))); } catch (Exception ex) { throw ex; } }
/// <summary> /// Appends the specified text to the file at the specified path. /// </summary> /// <param name="filename">A <see cref="String"/> representing the path of the file.</param> /// <param name="value">A <see cref="String"/> representing the text to append.</param> protected virtual void AppendText(string filename, string value) { lock (PadLock) { #if NETFX_CORE var folderTask = Windows.Storage.StorageFolder.GetFolderFromPathAsync(Device.File.DirectoryName(filename)).AsTask(); folderTask.Wait(); var fileTask = folderTask.Result.CreateFileAsync(Path.GetFileName(filename), Windows.Storage.CreationCollisionOption.OpenIfExists).AsTask(); fileTask.Wait(); Windows.Storage.FileIO.AppendTextAsync(fileTask.Result, value + Environment.NewLine).AsTask().Wait(); #elif SILVERLIGHT var store = ((FileSystem.SLFile)Device.File)._store; if (value.Length > store.AvailableFreeSpace && !((FileSystem.SLFile)Device.File).IncreaseStorage(value.Length + (store.Quota - store.AvailableFreeSpace))) { return; } System.IO.IsolatedStorage.IsolatedStorageFileStream fileStream = null; try { fileStream = store.OpenFile(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite); fileStream.Seek(0, SeekOrigin.End); using (var writer = new StreamWriter(fileStream, System.Text.Encoding.UTF8)) { writer.WriteLine(value); writer.Close(); } } finally { if (fileStream != null) { fileStream.Close(); fileStream.Dispose(); } } #else StreamWriter sw = null; try { FileStream fs = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite); fs.Seek(0, SeekOrigin.End); sw = new StreamWriter(fs, Encoding.UTF8); sw.WriteLine(value); } catch (Exception) { //Swallow any errors here... If we tried logging our error, we might end up back here. } finally { if (sw != null) { try { sw.Close(); } catch { } } //if (fs != null) //{ // //fs.Close(); // fs.Dispose(); //} } #endif } }