C# 테크닉 - 파일 제어( BIN format )
# WPF 를 이용하는 과정에서 HDD 에 존재하는 파일 ( binary format ) 제어
> Save, Load 형태 - List<Data> 형태 이용
> exe 파일 위치에 test.dat 파일 생성
# 소스 코드
using System.Runtime.Serialization.Formatters.Binary;
public class BINformat
{
[Serializable]
struct Data
{
public string key;
public string value;
public Data(string _key, string _value)
{
key= _key; value= _value;
}
}
public void WriteReadTest()
{
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
path = System.IO.Path.GetDirectoryName(path) + "\\test.dat";
List<Data> result;
List<Data> datalist = new List<Data>();
datalist.Add(new Data("11", "test1"));
datalist.Add(new Data("12", "test2"));
using (FileStream fs1 = new FileStream(path, FileMode.Create))
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(fs1, datalist);
}
using (FileStream fs2 = new FileStream(path, FileMode.Open))
{
BinaryFormatter bf2 = new BinaryFormatter();
result = (List<Data>)bf2.Deserialize(fs2);
}
}