即時創建一個TXT文檔,並寫入資料

摘要:即時創建一個TXT文檔,並寫入資料


protected void Button1_Click(object sender, EventArgs e)
    {
        FileStream fileStream = new FileStream(@"c:\test1.txt", FileMode.Create);

        fileStream.Close();   //切記開了要關,不然會被佔用而無法修改喔!!!

        using (StreamWriter sw = new StreamWriter(@"c:\test1.txt"))
        {
            // 欲寫入的文字資料 ~

            sw.Write("This is  ");
            sw.WriteLine("Shinyo Test lallalallaaaa.");
            sw.WriteLine("-------o   ---  V ---- o  -----");
            sw.Write("Time is : ");
            sw.WriteLine(DateTime.Now);
        }

    }

protected static void WriteToLog(string sLog)
        {
            FileStream fs = File.Open(@"Log.txt", FileMode.Append);

            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.WriteLine("--------------------------------------");
                sw.Write("Date : " + DateTime.Now);
                sw.WriteLine("Message : " + sLog);
            }

        }