C#文本写操作

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //方法一:写文本文件.txt
                StreamWriter writer = null;
                string filepath = "F:log.txt";
                string message = "方法一的写入";
                if (File.Exists(filepath))
                {
                    writer = File.AppendText(filepath);
                }
                else
                {
                    writer = File.CreateText(filepath);
                }
                writer.WriteLine("[" + DateTime.Now.ToString() + "]:" + message);
                writer.Close();
                //方法二:写文体文件.txt
                //File.AppendAllText(@"F:自作主张.txt", "方法二的写入");
            }
            catch
            {
                //把错误日志写到另一个文本文件中
                //writeError(e.Message);
            }
        }
    }
}

posted on 2008-03-17 11:50  zhanggang  阅读(1412)  评论(0编辑  收藏  举报