您的位置:首页 > 编程语言 > C#

C# 生成txt文件

2015-01-15 08:38 309 查看
     
///<summary>
/// 生成错误日志文件 (.txt)
/// 如果文件存在,则续写,不存在则创建
///</summary>
/// <paramname="path">文件路径</param>
/// <paramname="path">要写入的内容</param>
///<return>成功返回true</return>
public bool txtCreate(string path, stringcontent)
{
if(File.Exists(path))
{
StreamWriter sw = newStreamWriter(path, true, Encoding.Default);
sw.WriteLine(content);
sw.Flush();
sw.Close();
return true;
}
else
{
try
{
FileStream fs = new FileStream(path,FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs,Encoding.Default);
sw.Flush();
sw.BaseStream.Seek(0, SeekOrigin.Begin);
sw.WriteLine(content);
sw.Flush();
sw.Close();
return true;
}
catch (Exception ex)
{
throw ex;
return false;
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# txt