C#实现写入文本文件内容功能

protected void Page_Load(object sender, EventArgs e)
    {
        Write_Txt("kuukkk","yyyyy","klcfdnhh");
    }
    private void Write_Txt(string str1, string str2, string str3)
    {
        string strYMD = System.DateTime.Now.ToString("yyyyMMdd");
        string File_Name = @"D:\TestWeb\WriteTxt\" + strYMD + ".txt";//create a different file everyday.
        StreamWriter sr;
        if (File.Exists(File_Name))
        {
            sr = File.AppendText(File_Name);//追加文件內容
        }
        else
        {
            sr = File.CreateText(File_Name);
        }
        sr.WriteLine(str1 + "  " + str2 + "  " + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "  " + str3 + "\r\n");
        sr.Close();

    }

 

運行結果:

20110425.txt文檔

aaaaaa  bbbbbb  2008-04-25 01:51:38  cccccc

aaaaaa  bbbbbb  2008-04-25 01:52:01  cccccc

kuukkk  yyyyy  2008-04-25 01:52:40  klcfdnhh

 

 

你可能感兴趣的:(文本文件)