用怎样方法把一个C#(winform)系统,的任何用户的操作日志记录在一个文本中.
比如说 一个 显示日志格式 :
登陆时间 离开时间 年份,日期 年份,日期 登陆用户名
还有用什么方法调用上面的日志函数 在下基础不是很好,希望高手写出源码,,小弟感激涕零.
比如说 一个 显示日志格式 :
登陆时间 离开时间 年份,日期 年份,日期 登陆用户名
还有用什么方法调用上面的日志函数 在下基础不是很好,希望高手写出源码,,小弟感激涕零.
解决方案 »
- 关于datimepicker控件一个很诡异的问题
- C#中关于传递参数的问题
- C# windows 服务设置为’自动启动‘,但是在电脑启动的时候却没有启动,而手动是可以启动的,而且一切正常运行。
- c# winform中textbox怎么固定某些字符
- 如何使PictureBox的图片能够缩放,而不是用PictureBox的sizemode属性来进行图片缩放?
- 关于绑定后在checkboxlist items 加属性的难题?
- C#问题 急...
- 怎样能使表达式计算结果保留两位小数?
- 问一个菜问题
- C# restSharp cookie 登录
- 关于xml 排序 一问
- 能不能介绍一下静态函数与非静态函数的利弊????
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();
}
}
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();
} }
}