用怎样方法把一个C#(winform)系统,的任何用户的登陆时间与离开时间日志记录在一个文本中.(并加上登陆用户名)
用怎样方法把一个C#(winform)系统,的任何用户的操作日志记录在一个文本中.
比如说 一个 显示日志格式 :
登陆时间 离开时间
年份,日期 年份,日期 登陆用户名
还有用什么方法调用上面的日志函数
在下基础不是很好,希望高手写出源码,,小弟感激涕零.
问题点数:50、回复次数:4Top
1 楼LHA(心动)回复于 2006-03-17 21:58:00 得分 2
都是自己写的,时间日期 :DateTime.Now()Top
2 楼yufenfeila(雨纷飞啦)回复于 2006-03-17 22:09:41 得分 40
//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();
}
}Top
3 楼henry3695(henry(老师说学好正则可以赚美元的))回复于 2006-03-18 21:33:15 得分 8
根据楼上的师父,我测试了一下
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();
}
}
}
Top
4 楼sunscz(sun-_-_-)回复于 2006-03-18 23:48:47 得分 0
谢谢以上同仁,我已试了,在2005中通过,还在此基础上增加了许多东西,以上是客观公正的评分,谢谢Top




