public static void WriteFileTag(string filePath, string tag) { if (tag == null) { NativeFileOperationsHelper.DeleteFileFromApp($"{filePath}:files"); } else if (ReadFileTag(filePath) != tag) { NativeFileOperationsHelper.WriteStringToFile($"{filePath}:files", tag); } }
public virtual void ImportSettings(object import) { try { // Try convert settingsCache = (Dictionary <string, object>)import; // Serialize string serialized = JsonConvert.SerializeObject(settingsCache, Formatting.Indented); // Write to file NativeFileOperationsHelper.WriteStringToFile(settingsPath, serialized); } catch (Exception ex) { Debug.WriteLine(ex); Debugger.Break(); } }
public static void WriteFileTag(string filePath, string tag) { var isReadOnly = NativeFileOperationsHelper.HasFileAttribute(filePath, System.IO.FileAttributes.ReadOnly); if (isReadOnly) // Unset read-only attribute (#7534) { NativeFileOperationsHelper.UnsetFileAttribute(filePath, System.IO.FileAttributes.ReadOnly); } if (tag == null) { NativeFileOperationsHelper.DeleteFileFromApp($"{filePath}:files"); } else if (ReadFileTag(filePath) != tag) { NativeFileOperationsHelper.WriteStringToFile($"{filePath}:files", tag); } if (isReadOnly) // Restore read-only attribute (#7534) { NativeFileOperationsHelper.SetFileAttribute(filePath, System.IO.FileAttributes.ReadOnly); } }
protected virtual TValue Get <TValue>(TValue defaultValue, [CallerMemberName] string propertyName = "") { try { string settingsData = NativeFileOperationsHelper.ReadStringFromFile(settingsPath); Dictionary <string, TValue> rawData = JsonConvert.DeserializeObject <Dictionary <string, TValue> >(settingsData); Dictionary <string, object> convertedData = new Dictionary <string, object>(); if (rawData != null) { foreach (var item in rawData) { convertedData.Add(item.Key, (TValue)item.Value); } } serializableSettings = convertedData; if (serializableSettings == null) { serializableSettings = new Dictionary <string, object>(); } if (!serializableSettings.ContainsKey(propertyName)) { serializableSettings.Add(propertyName, defaultValue); // Serialize NativeFileOperationsHelper.WriteStringToFile(settingsPath, JsonConvert.SerializeObject(serializableSettings, Formatting.Indented)); } return((TValue)serializableSettings[propertyName]); } catch (Exception e) { Debugger.Break(); return(default(TValue)); } }
protected virtual bool Set <TValue>(TValue value, [CallerMemberName] string propertyName = "") { try { if (!serializableSettings.ContainsKey(propertyName)) { serializableSettings.Add(propertyName, value); } else { serializableSettings[propertyName] = value; } // Serialize NativeFileOperationsHelper.WriteStringToFile(settingsPath, JsonConvert.SerializeObject(serializableSettings, Formatting.Indented)); } catch (Exception e) { Debugger.Break(); return(false); } return(true); }
protected virtual TValue Get <TValue>(TValue defaultValue, [CallerMemberName] string propertyName = "") { try { // Check if caching is enabled if (isCachingEnabled) { // If the cache contains the setting... if (settingsCache.ContainsKey(propertyName)) { TValue settingValue; // Get the object object settingObject = settingsCache[propertyName]; // Check if it's a JToken object if (settingObject is JToken jtoken) { // Get the value from JToken settingValue = jtoken.ToObject <TValue>(); } else { // Otherwise, it is TValue, get the value settingValue = (TValue)settingObject; } // Return the setting and exit this function return(settingValue); } // Cache miss, the cache doesn't contain the setting, continue, to update the cache } // Read all settings from file string settingsData = NativeFileOperationsHelper.ReadStringFromFile(settingsPath); // If there are existing settings... if (!string.IsNullOrEmpty(settingsData)) { // Deserialize them and update the cache settingsCache = JsonConvert.DeserializeObject <Dictionary <string, object> >(settingsData); } // If it doesn't have this setting... if (!settingsCache.ContainsKey(propertyName)) { // Add it to cache settingsCache.Add(propertyName, defaultValue); // Serialize with updated value string serialized = JsonConvert.SerializeObject(settingsCache, Formatting.Indented); // Write to file NativeFileOperationsHelper.WriteStringToFile(settingsPath, serialized); } // Get the value object object valueObject = settingsCache[propertyName]; if (valueObject is JToken jtoken2) { return(jtoken2.ToObject <TValue>()); } return((TValue)valueObject); } catch (Exception ex) { Debug.WriteLine(ex); Debugger.Break(); return(default);
public bool WriteToFile(string json) { return(NativeFileOperationsHelper.WriteStringToFile(_filePath, json)); }