public static string LoadLocalData(string fileName) { string fullPath = CUtils.PathCombine(CUtils.GetRealPersistentDataPath(), fileName); if (System.IO.File.Exists(fullPath)) { FileStream fs = new FileStream(fullPath, FileMode.Open); if (fs != null && fs.Length > 0) { byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); fs.Close(); string loadData = string.Empty; try { loadData = Encoding.UTF8.GetString(CryptographHelper.Decrypt(bytes, key, iv)); } catch (System.Exception ex) { Debug.LogError(ex); } return(loadData); } } return(""); }
public static string LoadLocalData(string fileName) { string fullPath = CUtils.PathCombine(CUtils.GetRealPersistentDataPath(), fileName); FileInfo fileInfo = new FileInfo(fullPath); string loadData = string.Empty; if (fileInfo.Exists) { using (FileStream fs = fileInfo.OpenRead()) { byte[] bytes = new byte[fs.Length]; fs.Read(bytes, 0, bytes.Length); try { loadData = Encoding.UTF8.GetString(CryptographHelper.Decrypt(bytes, key, iv)); } catch (System.Exception ex) { Debug.LogError(ex); } return(loadData); } } return(loadData); }
/// <summary> /// Deletes the persistent file. /// </summary> /// <param name="fileName">File name.</param> public static void DeletePersistentFile(string fileName) { string path = CUtils.PathCombine(CUtils.GetRealPersistentDataPath(), fileName); if (File.Exists(path)) { File.Delete(path); } }
/// <summary> /// Reads the persistent file. /// </summary> /// <returns>The persistent file.</returns> /// <param name="fileName">File name.</param> public static byte[] ReadPersistentFile(string fileName) { string path = CUtils.PathCombine(CUtils.GetRealPersistentDataPath(), fileName); if (File.Exists(path)) { return(File.ReadAllBytes(path)); } else { return(null); } }
/// <summary> /// 本地存储 /// </summary> public static bool SaveLocalData(string fileName, string saveData) { string fullPath = CUtils.PathCombine(CUtils.GetRealPersistentDataPath(), fileName); FileStream fs = new FileStream(fullPath, FileMode.Create); if (fs != null) { byte[] bytes = CryptographHelper.Encrypt(Encoding.UTF8.GetBytes(saveData), key, iv); fs.Write(bytes, 0, bytes.Length); fs.Flush(); fs.Close(); return(true); } return(false); }
/// <summary> /// 本地存储 /// </summary> public static bool SaveLocalData(string fileName, string saveData) { string fullPath = CUtils.PathCombine(CUtils.GetRealPersistentDataPath(), fileName); FileInfo fileInfo = new FileInfo(fullPath); if (!fileInfo.Directory.Exists) { fileInfo.Directory.Create(); } // if (!fileInfo.Exists) fileInfo.Create(); using (var sw = fileInfo.OpenWrite()) { byte[] bytes = CryptographHelper.Encrypt(Encoding.UTF8.GetBytes(saveData), key, iv); sw.Write(bytes, 0, bytes.Length); sw.Flush(); } return(true); }
/// <summary> /// Changes the name of the persistent file. /// </summary> /// <returns><c>true</c>, if persistent file name was changed, <c>false</c> otherwise.</returns> /// <param name="oldName">Old name.</param> /// <param name="newname">Newname.</param> public static bool ChangePersistentFileName(string oldName, string newname) { string path = CUtils.GetRealPersistentDataPath(); string oldPath = CUtils.PathCombine(path, oldName); string newPath = CUtils.PathCombine(path, newname); if (File.Exists(oldPath)) { FileInfo newFile = new FileInfo(newPath); //如果新的存在需要删除 if (newFile.Exists) { newFile.Delete(); } FileInfo finfo = new FileInfo(oldPath); finfo.MoveTo(newPath); return(true); } return(false); }
/// <summary> /// Saves the file. /// </summary> /// <returns>The file.</returns> /// <param name="context">Context.</param> /// <param name="fileName">File name.</param> public static void SavePersistentFile(Array context, string fileName) { string path = CUtils.PathCombine(CUtils.GetRealPersistentDataPath(), fileName); FileInfo finfo = new FileInfo(path); if (!finfo.Directory.Exists) { finfo.Directory.Create(); } using (StreamWriter sw = new StreamWriter(path, false)) { sw.BaseStream.Write((byte[])context, 0, context.Length); sw.Flush(); } #if UNITY_EDITOR Debug.LogFormat("save file path={0},len={1}", path, context.Length); #endif }
/// <summary> /// Delete the persistent Directory /// </summary> public static void DeletePersistentDirectoryFiles(string relative = null) { string path = CUtils.GetRealPersistentDataPath(); if (!string.IsNullOrEmpty(relative)) { path = CUtils.PathCombine(path, relative); } DirectoryInfo dinfo = new DirectoryInfo(path); if (dinfo.Exists) { var allFiles = dinfo.GetFiles("*", SearchOption.AllDirectories); FileInfo fino; for (int i = 0; i < allFiles.Length; i++) { fino = allFiles[i]; fino.Delete(); } ; // dinfo.Delete(true); } }
/// <summary> /// the Persistents file is Exists. /// </summary> /// <returns><c>true</c>, if file was persistented, <c>false</c> otherwise.</returns> /// <param name="abpath">Abpath.</param> public static bool PersistentFileExists(string abpath) { string path = CUtils.PathCombine(CUtils.GetRealPersistentDataPath(), abpath); return(File.Exists(path)); }