[재무재표] 위메이드 사업보고서(2020.12)

읽으면 진짜 재무제표 보이는 책   위메이드 사업보고서(2020.12) [재무상태표] 2020년 12월 31일 현재 (단위: 십억원) 자산 220 부채 22 자본 198

[C#] File to Byte Array, Byte Array to File


<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;
}

댓글

댓글 쓰기

이 블로그의 인기 게시물

[C#][System.IO.Directory.GetFiles] 지정된 폴더의 파일목록 가져오기

[WPF][WebBrowser] Allow Blocked Content Setting