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();

 }

posted @ 2014-11-18 17:15  @睦  阅读(3901)  评论(0编辑  收藏  举报