2010年12月30日 星期四

寫入純文字檔

using System.IO;
private void WriteText(String filePath,String text)
{
FileStream fs=null ;
StreamWriter writer = null;
try
{
if( File.Exists(filePath))
{
fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
}
else
{
fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);
}
writer = new StreamWriter(fs);
writer.WriteLine(text);
writer.Flush();
fs.Flush();
}
catch (Exception Ex)
{
Console.WriteLine(Ex.Message);
}
finally
{
if(fs!=null) fs.Close();
}
}

沒有留言: