/// <summary> /// Reads Json binary file and returns it as a string. /// If the file cannot be found, the method returns null. /// </summary> /// <param name="fullFilePath"></param> /// <returns></returns> public static string ReadJsonFromFile(string fullFilePath) { string result = null; try { JsonSerializer jsonSerializer = new JsonSerializer(); if (File.Exists(fullFilePath)) { BsonReader bReader = new BsonReader(new BinaryReader(fullFilePath)); result = jsonSerializer.Deserialize <object>(bReader).ToString(); bReader.Close(); } else { return(null); } } catch (Exception ex) { SyncProMethods.LogException(@"SyncProMethods \ ReadJsonFromFile - Failed to read file with exception - ", ex); return(null); } return(result); }
/// <summary> /// Write Json object to file /// </summary> /// <param name="fullFilePath"></param> /// <param name="jsonObject"></param> public static void WriteJsonToFile(string fullFilePath, object jsonObject) { try { JsonSerializer jsonSerializer = new JsonSerializer(); BsonWriter bWriter = new BsonWriter(new BinaryWriter(new FileStream(fullFilePath, FileMode.Create, FileAccess.Write))); jsonSerializer.Serialize(bWriter, jsonObject); bWriter.Close(); } catch (Exception ex) { SyncProMethods.LogException(@"SyncProMethods \ WriteJsonToFile - Failed to read file with exception - ", ex); } }
/// <summary> /// Creates directory to hold all Configurations files. /// </summary> /// <returns></returns> public static DirectoryInfo CreateConfigurationsFilesDirectory() { string path = Directory.GetApplicationRootDirectory() + Path.DirectorySeparatorChar + "NVRAM" + Path.DirectorySeparatorChar + "Configurations" + Path.DirectorySeparatorChar; try { if (!Directory.Exists(path)) { return(Crestron.SimplSharp.CrestronIO.Directory.CreateDirectory(path)); } return(new DirectoryInfo(path)); } catch (Exception ex) { SyncProMethods.LogException(@"FileOperations \ CreateConfigurationsDirectories failes with - ", ex); return(null); } }