当前位置:文档之家› C#实现日志文件清除功能-4页word资料

C#实现日志文件清除功能-4页word资料

C#实现日志文件清除功能-4页word资料
C#实现日志文件清除功能-4页word资料

C#实现日志文件清除功能

C# Log File Removal Function

LIU De-jun

(Information Department, Sheyang County People's Hospital of Jiangsu ,Yancheng 224300,China)

Abstract: The log file is recorded file system operation event,operating system operating system log files, database system database, the system log file. The system log file contains a system message files, including kernel, service, an application running on the system. Different log files record different information. This paper aimed at the hospital information system in the application log file long time accumulation leads to the problem of disk space is full, puts forward solving methods specific log file cleared.

我院信息系统在运行过程,会对客户端操作员的操作进行日志文件记录,随着时间推移,日志文件越来越多,导致磁盘空间满,程序运行报错。考虑到工作站众多,手动清除不现实,因此考虑通过程序让工作站操作人员来完成清除工作,经过多次测试,终于实现了日志文件清除功能。

1 需求

该功能的需求如下:1)要清除的日志文件名称含有日期格式(如:debugSql20131115.log、YB2014-02-18.txt等);2)要保留近6个月的日志文件备查;3)按指定格式的文件删除。4)因清除文件格一定且数

量较多,所以系统要支持通配符(*)方式清除。4)程序对当前所在目录进行处理。

2 方法

通过使用C#的文件操作功能,对指定格式的文件名与程序所在目录的文件进行比对,检索出日志文件名中的日期在6个月前的文件,对检索到的文件执行删除操作。

3 详细步骤

3.1 建立C#应用程序

建立界面如图1,控件参数如表1的应用程序界面(项目名称为:日志清除工具)。

*_Speak.LOG

Err*.log

NH*.txt

printer*.log

YB*.txt\&]

3.2 编写代码

using System;

using System.Collections.Generic;

using SystemponentModel;

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)

{string Tdir;

string sFilename;

Tdir = System.IO.Directory.GetCurrentDirectory();

Int32 j;

j= 0;

this.toolStripStatusLabel1.Text="正在查找文件......";

for (Int32 i = 0; i dt = DateTime.Today;

DateTime dt1 = dt.AddMonths(-6);

string dt4;

if (ff.Length != 0) //文件不为空

{foreach (FileInfo fi in ff) //遍历目录中的指定的文件名,可带通配符*

{string tFilename;

switch (i) //格式为debugsql*.log,如debugsql20130101.log {case 0:

相关主题
文本预览
相关文档 最新文档