<File to Byte Array>
1. File Stream을 이용하여 파일을 Byte 배열로 변환
using System.IO; ... public byte[] FileToByteArray(string path) { byte[] fileBytes = null; try { using(FileStream fileStream = new FileSystem(path, FileMode.Open)) { fileBytes = new byte[fileStream.Length]; fileStream.Read(fileBytes, 0, fileBytes.Length); } } catch (Exception e) { // Exception ... } return fileBytes; }
<Byte Array to File>
1. FileStream을 사용하여 Byte 배열을 파일로 변환
using System.IO; ... public bool ByteArrayToFile(string path, byte[] buffer) { try { using(FileStream fileStream = new FileSystem(path, FileMode.Create)) { fileStream.Write(buffer, 0, buffer.Length); } return true; } catch (Exception e) { // Exception ... } return false; }
2. File.WriteAllBytes를 이용하여 Byte 배열을 파일로 변환
using System.IO; ... public bool ByteArrayToFile2(string path, byte[] buffer) { try { File.WriteAllBytes(path, buffer); return true; catch (Exception e) { // Exception ... } return false; }