1.

string path = @"D:\log.txt";

if (!System.IO.File.Exists(path))

{

    //create file

    using (System.IO.FileStream fs = System.IO.File.Create(path))

        fs.Close();//release resource

}

System.IO.StreamWriter file = new System.IO.StreamWriter(path, true);//true表示不覆蓋data

file.WriteLine("log...");//write data

file.WriteLine("------------------------------------------------");//分隔線

file.Close();

2.

string path = @"C:\" + DateTime.Now.ToString("yyyyMMdd") + ".txt"; ;

if (!System.IO.File.Exists(path))//若沒有該檔案救create

{

    System.IO.FileStream fs = System.IO.File.Create(path);

    fs.Close();//release resource

}

using (System.IO.StreamWriter sw = System.IO.File.AppendText(path))

{

    sw.Write("...");//不換行寫入String

    sw.WriteLine("...");//換行寫入String

    sw.WriteLine("-------------------------------------------");//分隔線

    sw.Flush();

    sw.Close();//release resource

}

arrow
arrow
    全站熱搜

    Big Bear 發表在 痞客邦 留言(0) 人氣()