
base64를 통해 파일을 바이트 혹은 "텍스트" 형태로 주고 받을 수 있습니다.
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
namespace Base64
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            //텍스트가 길어질 수 있으니 늘려줍니다.
            //혹은 파일을 읽어도 괜찮겠네요.
            textBox1.MaxLength = 100000000;
        }
        private void button_fileToBase64_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                string filePath = openFileDialog.FileName;
                byte[] fileBytes = File.ReadAllBytes(filePath); //바이트로 파일을 읽어온 후
                char[] fileToBase64 = new char[(int)Math.Ceiling(fileBytes.Length * 1.5)]; //base64는 33% 증가!
                Convert.ToBase64CharArray(fileBytes, 0, fileBytes.Length, fileToBase64, 0); //base64로 만들어줍니다.
                MessageBox.Show(fileBytes.Length + " " + fileToBase64.Length);
                Clipboard.SetText(new string(fileToBase64)); //클립보드에 복사해줍니다. string으로 만들어서!
            }
        }
        private void button_base64ToFile_Click(object sender, EventArgs e)
        {
            string text = textBox1.Text;
            byte[] textBytes = Convert.FromBase64String(text); //위의 string을 다시 base64 byte로 만들어줍니다.
            string folder = Directory.GetCurrentDirectory();
            string filePath = Path.Combine(folder, "file");
            File.WriteAllBytes(filePath, textBytes); //해당 값으로 파일을 만들어줍니다.
            Process.Start(folder); //파일 생성 확인용 폴더 띄워주기
        }
    }
}'C# > Windows Form' 카테고리의 다른 글
| [C# Windows Form] 로그 클래스 (CallerMemberName) (0) | 2024.02.17 | 
|---|---|
| [C# Windows Form] Close, Application.Exit, Environment.Exit 차이점 (0) | 2024.02.17 | 
| [C# Windows Form] Form을 Alt+Tab 에 안보이도록 설정 (0) | 2024.02.11 | 
| [C# Windows Form] 투명 오브젝트 만들기 (워터마크, 덮개 등) (0) | 2024.02.11 | 
| [C# Windows Form] 파일명 필터 걸기 (OpenFileDialog Filter) (0) | 2024.01.30 |