CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
IBM Rational 系统开发最佳实践工具包 WebSphere MQ 最佳实践 TOP 15
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

用怎样方法把一个C#(winform)系统,的任何用户的登陆时间与离开时间日志记录在一个文本中.(并加上登陆用户名)

楼主sunscz(sun-_-_-)2006-03-17 21:09:10 在 .NET技术 / C# 提问

用怎样方法把一个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

相关问题

  • 用c#如何读取系统日志?
  • 记录日志 是用mysql好,还是用文本好?
  • 文本框(C#实现)
  • C# 中的文本框?
  • C#制作文本文件
  • 如何将tomcat后台日志输出到一个文本文件里
  • Html中用Size来给定文本框的宽度而在C#(WinForm)中怎样做到同样的效果呢?????
  • 请教一下,在C#使用WinForm时如何让文本框中默认有下划线的存在
  • 如何得到C#多行文本框中文本的行数?
  • 文本分析!(C++)(高手请进!)

关键词

  • c#
  • 用户
  • 代码
  • logouttime
  • 日志
  • logintime
  • 登陆
  • textbox
  • 方法
  • sw

得分解答快速导航

  • 帖主:sunscz
  • LHA
  • yufenfeila
  • henry3695

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo