用怎样方法把一个C#(winform)系统,的任何用户的操作日志记录在一个文本中.
 比如说 一个 显示日志格式 :             
                           登陆时间       离开时间                          年份,日期       年份,日期       登陆用户名
还有用什么方法调用上面的日志函数                   在下基础不是很好,希望高手写出源码,,小弟感激涕零.

解决方案 »

  1.   

    都是自己写的,时间日期 :DateTime.Now()
      

  2.   

    //C#2.0
    public class Form1 : Form
    {
        string user = "";
        DateTime loginTime, logoutTime;    public Form1()
        {
            InitializeComponent();
        }    private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                user = textBox1.Text;
                loginTime = DateTime.Now;
                MessageBox.Show("登陆成功");
            }
        }    private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (user != "")
            {
                logoutTime = DateTime.Now;
                WriteLog();
            }
        }    private void WriteLog()
        {
            StreamWriter sw = new StreamWriter("C:\\log.txt", true);
            sw.WriteLine(loginTime.ToString() + "\t" + logoutTime.ToString() + "\t" + user);
            sw.Close();
        }
    }
      

  3.   

    根据楼上的师父,我测试了一下VS2003测试通过了using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.IO;namespace WinFormTest
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Button button1;
    string user = "";
    DateTime loginTime, logoutTime;
    private System.Windows.Forms.TextBox textBox1; /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.button1 = new System.Windows.Forms.Button();
    this.textBox1 = new System.Windows.Forms.TextBox();
    this.SuspendLayout();
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(80, 104);
    this.button1.Name = "button1";
    this.button1.TabIndex = 0;
    this.button1.Text = "button1";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // textBox1
    // 
    this.textBox1.Location = new System.Drawing.Point(80, 56);
    this.textBox1.Name = "textBox1";
    this.textBox1.TabIndex = 1;
    this.textBox1.Text = "henry3695";
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(264, 182);
    this.Controls.Add(this.textBox1);
    this.Controls.Add(this.button1);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Closed += new System.EventHandler(this.Form1_Closed);
    this.ResumeLayout(false); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    } private void button1_Click(object sender, System.EventArgs e)
    {
    if (textBox1.Text != "")
    {
    user = textBox1.Text;
    loginTime = DateTime.Now;
    MessageBox.Show("登陆成功");
    } } private void Form1_Closed(object sender, System.EventArgs e)
    {
    if (user != "")
    {
    logoutTime = DateTime.Now;
    WriteLog();
    } } private void WriteLog()
    {
    StreamWriter sw = new StreamWriter("C:\\log.txt", true);
    sw.WriteLine(loginTime.ToString() + "\t" + logoutTime.ToString() + "\t" + user);
    sw.Close();
    } }
    }