C# 创建text文本并写入数据

/// <summary>

/// 创建TXT文本并往里面写入数据

/// </summary>

/// <param name="FileName">要创建的文件名,不要后缀名</param>

protected void WriteTxt(string FileName)

{

  Encoding code = Encoding.GetEncoding("gb2312"); //编码格式

string htmlfilename = HttpContext.Current.Server.MapPath("/txt/" + FileName + ".txt"); //文件保存路径

string str = ""; //写入内容

StreamWriter sw = null;

{

  try

     {

       sw = new StreamWriter(htmlfilename, false, code);

  sw.Write(str);

 sw.Flush();

  }

     catch { }

  }

  sw.Close();

  sw.Dispose();

}