protected void Button1_Click(object sender, EventArgs e)
    {
        using (StreamWriter w = File.AppendText("log.txt"))//追边文件
        {
            Log("Test1", w);
            Log("Test2", w);
            // Close the writer and underlying file.
            w.Close();
        }
        // Open and read the file.
        using (StreamReader r = File.OpenText("log.txt"))//读取文件
        {
            DumpLog(r);
        }


    }

public static void Log(string logMessage, TextWriter w)//
    {
        w.Write("\r\nLog Entry : ");
        w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(),
            DateTime.Now.ToLongDateString());
        w.WriteLine("  :");
        w.WriteLine("  :{0}", logMessage);
        w.WriteLine("-------------------------------");
        // Update the underlying file.
        w.Flush();
    }

    public static void DumpLog(StreamReader r)
    {
        // While not at the end of the file, read and write lines.
        string line;
        while ((line = r.ReadLine()) != null)
        {
            Console.WriteLine(line);
           
        }
        r.Close();
    }

posted on 2011-05-06 15:11  lucky.dai  阅读(745)  评论(0)    收藏  举报