標籤:blog os io 檔案 ar div amp log
//================二進位相關轉換類============== #region 將檔案轉換為位元組 /// <summary> /// 將檔案轉換為位元組 /// </summary> /// <param name="FilePath">檔案完整路徑</param> /// <returns>位元組</returns> public static byte[] FileToBinary(string FilePath) { byte[] Buffer = null; if (Utils.FilesHelper.FileExists(FilePath) && System.IO.Path.HasExtension(FilePath)) { FileStream stream = new FileInfo(FilePath).OpenRead(); Buffer = new byte[stream.Length]; stream.Read(Buffer, 0, Convert.ToInt32(stream.Length)); } return Buffer; } #endregion #region 位元組轉為檔案 /// <summary> /// 位元組轉為檔案 /// </summary> /// <param name="FilePath">轉到的檔案完整路徑</param> /// <param name="Buffer">位元組</param> /// <returns>轉換是否成功</returns> public static bool BinaryToFile(string FilePath, byte[] Buffer) { bool flag = false; FileStream fstream = File.Create(FilePath, Buffer.Length); try { fstream.Write(Buffer, 0, Buffer.Length); flag = true; } catch (Exception) { } finally { fstream.Close(); } return flag; } #endregion