file access, file을 생성하고 읽어보자
목표: File을 생성하거나 파일 내용을 읽어오는 코딩을 해보자.
using System.IO;
1. File Read 작업순서:
/// 파일을 읽어서 richtxt에 보여준다.
/// FuncName: btnSummaryView_Click
/// review : This function is view after read the Summary file
/// apply : file access method,
/// ////////////////////////////////////////////////////////////
private void btnSummaryView_Click(object sender, EventArgs e)
{
if (m_bOpenfile != true)
{
MessageBox.Show("It is not set to see the Summary file!");
return;
}
//richtxtSummaryView.LoadFile(axDBS.strFilePath);
// Read the summary files
string strOneLine = null, strTotalLine = null;
try
{
FileStream aFile = new FileStream(axDBS.strFilePath, FileMode.Open);
StreamReader sr = new StreamReader(aFile);
strOneLine = sr.ReadLine();
// Read the One line from the opened this file.
while (strOneLine != null)
{
strTotalLine += strOneLine + "\r\n";
strOneLine = sr.ReadLine();
}
sr.Close();
richtxtSummaryView.Text = strTotalLine;
}
catch (IOException err)
{
MessageBox.Show(err.ToString());
return;
}
}
2. File Write 작업순서
/// FuncName: btnSummarySave_Click
/// review : This function is view after read the Summary file
/// apply : file access method,
/// ///////////////////////////////////////////////////////////////
private void btnSummarySave_Click(object sender, EventArgs e)
{
if (m_bOpenfile != true)
{
MessageBox.Show("It is not set to see the Summary file!");
return;
}
if (richtxtSummaryView.Text == "")
{
MessageBox.Show("Context of the Summary File is not exist!");
return;
}
FileStream aFile = new FileStream(axDBS.strFilePath, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
sw.Write(richtxtSummaryView.Text);
sw.Close();
}