// // FileMgr.cs // // Author: // JasonXuDeveloper(傑) // // Copyright (c) 2020 JEngine // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. using System; using System.IO; using UnityEngine; namespace JEngine.Core { public static partial class FileMgr { /// /// 是否存在文件 /// /// /// public static bool HasFile(string path) { return File.Exists(path); } /// /// 删除文件或目录 /// /// public static void Delete(string path) { if (File.Exists(path)) { File.Delete(path); } if (!Directory.Exists(path)) return; var di = new DirectoryInfo(path); di.Delete(true); } /// /// 将文件转换成byte[]数组 /// /// 文件路径文件名称 /// byte[]数组 public static byte[] FileToBytes(string fileUrl) { try { return File.ReadAllBytes(fileUrl); } catch(Exception ex) { Debug.LogException(ex); return null; } } /// /// 将byte[]数组保存成文件 /// /// byte[]数组 /// 保存至硬盘的文件路径 /// public static bool BytesToFile(byte[] byteArray, string fileName) { var result = true; try { File.WriteAllBytes(fileName, byteArray); } catch(Exception ex) { Debug.LogException(ex); result = false; } return result; } } }