[HowTo]파일에 문자열을 기록하는 코드(C#)
private void WriteTempLog(string contents)
{
string strFileName = "c:\\Navigator.txt";
System.IO.FileStream oFS = null;
System.IO.StreamWriter oSW = null;
string strLogContents = "";
try
{
//파일스트림 객체 초기화
oFS = new System.IO.FileStream(
//파일 이름 지정
strFileName,
//파일이 있으면 열고, 없으면 만든다
System.IO.FileMode.OpenOrCreate,
//파일을 읽기,쓰기 모드로 연다
System.IO.FileAccess.ReadWrite);
//스트림라이터 객체 초기화
oSW = new System.IO.StreamWriter(oFS);
//마지막 부분을 찾는다.
oSW.BaseStream.Seek(0, System.IO.SeekOrigin.End);
strContents = contents;
oSW.Write(strContents);
//반드시 flush를 해야, 메모리에 있는 내용을 파일에 기록한다.
//flush하지 않으면 파일을 잠그기 때문에 다른 프로세스가 이 파일에 접근할 수 없다
oSW.Flush();
oSW.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
oSW = null;
oFS = null;
}