클래스나 구조체 같은 복잡한 이진 데이터를 저장할때 사용하는 방법.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO; // 파일 스트림 처리

namespace OpenFile
{
    public partial class Form1 : Form
    {
        Human Kim = new Human("김용연", 28);
        public Form1()
        {
            InitializeComponent();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(@"c:\kim.bin", FileMode.Create, FileAccess.Write);
            BinaryWriter bw = new BinaryWriter(fs);
            Kim.Write(bw);
            fs.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "파일을 읽는 중";
            textBox1.Refresh();
            System.Threading.Thread.Sleep(1000);
            FileStream fs = new FileStream(@"c:\kim.bin", FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            Kim = Human.Read(br);
            fs.Close();
            textBox1.Text = Kim.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            textBox1.Text = Kim.ToString();
        }
    }
    class Human
    {
        private string Name;
        private int Age;
        private float Temp;
        public Human(string aName, int aAge)
        {
            Name = aName;
            Age = aAge;
            Temp = 1.23f;
        }
        public override string ToString()
        {
            Temp += 1;
            return "이름:" + Name + ",나이:" + Age;
        }
        public void Write(BinaryWriter bw)
        {
            bw.Write(Name);
            bw.Write(Age);
        }
        public static Human Read(BinaryReader br)
        {
            return new Human(br.ReadString(), br.ReadInt32());
        }
    }
}

'객체지향언어 > C#' 카테고리의 다른 글

FileWatcher 예제  (0) 2009.08.09
GetFiles 예제  (0) 2009.08.09
메모장 흉내내기~  (0) 2009.08.08
Stream을 이용한 파일 입출력 예제  (0) 2009.08.08
File Open, Folder Open 예제  (0) 2009.08.08

입출력 과정에서 자동으로 문자열 변환을 수행하는 StreamReader, StreamWriter 사용하는 기본예제...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO; // 파일 스트림 처리

namespace OpenFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            StreamWriter sw = new StreamWriter(@"c:\cstest.txt");
            sw.Write(textBox1.Text);
            sw.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            char[] buf = new char[1024];
            int ret;
            StreamReader sr = new StreamReader(@"c:\cstest.txt");
            textBox1.Text = "";
            while (true)
            {
                ret = sr.Read(buf, 0, 1024);
                textBox1.Text += new string(buf, 0, ret);
                if (ret < 1024) break;
            }
            sr.Close();
        }
    }
}

'객체지향언어 > C#' 카테고리의 다른 글

GetFiles 예제  (0) 2009.08.09
이진파일 저장 예제  (0) 2009.08.08
Stream을 이용한 파일 입출력 예제  (0) 2009.08.08
File Open, Folder Open 예제  (0) 2009.08.08
프로세스 열거하기  (0) 2009.08.08

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.IO; // 파일 스트림 처리

namespace OpenFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            byte[] data = { 65, 66, 67, 68, 69, 70, 71, 72 };

            FileStream fs = new FileStream(@"c:\fs.txt", FileMode.Create, FileAccess.Write);
            fs.Write(data, 0, data.Length);
            fs.Close();
            MessageBox.Show(@"C:의 fs.txt 파일에 기록함");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] data = new byte[8];

            try
            {
                FileStream fs = new FileStream(@"c:\fs.txt", FileMode.Open, FileAccess.Read);
                fs.Read(data, 0, data.Length);
                fs.Close();

                string result = "";
                for (int i = 0; i < data.Length; i++)
                {
                    result += data[i].ToString() + ",";
                }
                MessageBox.Show(result, "파일내용");
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show("지정한 파일이 없습니다.");
            }
        }
    }
}

'객체지향언어 > C#' 카테고리의 다른 글

이진파일 저장 예제  (0) 2009.08.08
메모장 흉내내기~  (0) 2009.08.08
File Open, Folder Open 예제  (0) 2009.08.08
프로세스 열거하기  (0) 2009.08.08
Thread Test  (0) 2009.08.08

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OpenFile
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = "C:\\";
            openFileDialog1.Multiselect = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                foreach (string file in openFileDialog1.FileNames)
                {
                    MessageBox.Show(file + "를 선택했습니다.");
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(folderBrowserDialog1.SelectedPath + "를 선택했습니다.");
            }
        }
    }
}

'객체지향언어 > C#' 카테고리의 다른 글

이진파일 저장 예제  (0) 2009.08.08
메모장 흉내내기~  (0) 2009.08.08
Stream을 이용한 파일 입출력 예제  (0) 2009.08.08
프로세스 열거하기  (0) 2009.08.08
Thread Test  (0) 2009.08.08

using System;
using System.Diagnostics;

class CSTest
{
    static void Main()
    {
        Process[] Procs = Process.GetProcesses();
        foreach (Process p in Procs)
        {
            Console.WriteLine("ID = {0,5}, 이름 = {1}", p.Id, p.ProcessName);
        }
    }
}
 

'객체지향언어 > C#' 카테고리의 다른 글

이진파일 저장 예제  (0) 2009.08.08
메모장 흉내내기~  (0) 2009.08.08
Stream을 이용한 파일 입출력 예제  (0) 2009.08.08
File Open, Folder Open 예제  (0) 2009.08.08
Thread Test  (0) 2009.08.08

using System;
using System.Threading;

class CSTest
{
    // 작업 스레드
    static void ThreadProc()
    {
        for (int i=0; i<10; i++)
        {
            Console.WriteLine(i);
            Thread.Sleep(500);
        }
        Console.WriteLine("작업 스레드 종료");
    }
    //주 스레드
    static void Main()
    {
        Thread T = new Thread(new ThreadStart(ThreadProc));
        T.Start();
        for(;;)
        {
            ConsoleKeyInfo cki;
            cki = Console.ReadKey();
            if (cki.Key == ConsoleKey.A)
            {
                Console.Beep();
            }
            if (cki.Key == ConsoleKey.B)
            {
                break;
            }
        }
        Console.WriteLine("주 스레드 종료");
    }
}
 


 

'객체지향언어 > C#' 카테고리의 다른 글

이진파일 저장 예제  (0) 2009.08.08
메모장 흉내내기~  (0) 2009.08.08
Stream을 이용한 파일 입출력 예제  (0) 2009.08.08
File Open, Folder Open 예제  (0) 2009.08.08
프로세스 열거하기  (0) 2009.08.08

+ Recent posts