protected void btnOpen_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFileName.Text)) txtFileName.Text="c:/temp/temp.txt";
StreamReader sr = null;
try
{
sr = new StreamReader(txtFileName.Text);
txtFileContent.Text = sr.ReadToEnd();
sr.Close();
displayMessage("Successfully opened file");
}
catch (Exception ex)
{
displayMessage(ex.Message);
}
finally
{
sr.Close();
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtFileName.Text)) txtFileName.Text = "c:/temp/temp.txt";
StreamWriter sw = null;
try{
sw = new StreamWriter(txtFileName.Text);
string[] ar;
ar = txtFileContent.Text.Split(Environment.NewLine.ToCharArray());
foreach (string sIn in ar)
{
if(!string.IsNullOrEmpty(sIn))
sw.Write(sIn.Trim()+Environment.NewLine);
}
}catch (Exception ex)
{
displayMessageLine(ex.Message);
}
finally{
sw.Close();
}
}
Programming Journal C#, Java, SQL and to a lesser extent HTML, CSS, XML, and regex. I made this so other programmers could benefit from my experience.
Monday, June 23, 2008
Opening, Reading, and Writing a file
Here is an example of Opening/Reading a file in btnOpen_Click and Writing in btnSave_Click:
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment