C#读写日志文本文件
日志为文本文件
每列以制表符隔开 行以换行符隔开
本次示例简单实现如下相关功能:
1.正写日志文本 最新的日志放后面
2.倒写日志文本 最新的日志放前面
3.读日志文本内容显示在Label
4.读日志文本内容到DataTable 及 筛选后显示在GridView
--------------------
(以下操作并没有考虑相关如文件不存在等异常)
//1.正写日志 最新日志放最后面
protected void Button1_Click(object sender, EventArgs e)
{
string strFilePath = Server.MapPath("log/log_200807_1.txt");
System.IO.FileStream fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append);
System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);
sw.WriteLine("'" + DateTime.Now.ToString() + "'\t'zhangsan'\t'Login.aspx'\t'登录A'");
sw.Close();
fs.Close();
}
//2.倒写日志 最新日志放最前面
protected void Button2_Click(object sender, EventArgs e)
{
string strFilePath = Server.MapPath("log/log_200807_1.txt");
string strOldText = File.ReadAllText(strFilePath, System.Text.Encoding.Default);
File.WriteAllText(strFilePath, "'" + DateTime.Now.ToString() + "'\t'zhangsan'\t'Login.aspx'\t'登录B'\r\n", System.Text.Encoding.Default);
File.AppendAllText(strFilePath, strOldText, System.Text.Encoding.Default);
}
//3.读日志文本到Label
protected void Button3_Click(object sender, EventArgs e)
{
string strFilePath = Server.MapPath("log/log_200807_1.txt");
FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
string strLine = sr.ReadLine();
string str = "";
while (strLine != null)
{
str += strLine.ToString() + "<br/>";
strLine = sr.ReadLine();
}
sr.Close();
fs.Close();
this.Label1.Text = str;
}
//4.读日志文本内容到DataTable及筛选后显示在GridView
protected void Button4_Click(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("日志时间");
dt.Columns.Add("操作人员");
dt.Columns.Add("日志页面");
dt.Columns.Add("日志内容");
string strFilePath = Server.MapPath("log/log_200807_1.txt");
FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
string strLine = sr.ReadLine();
while (strLine != null)
{
string[] strArray = new string[4];
strArray = strLine.Split('\t');
DataRow dr = dt.NewRow();
dr[0] = strArray[0];
dr[1] = strArray[1];
dr[2] = strArray[2];
dr[3] = strArray[3];
dt.Rows.Add(dr);
strLine = sr.ReadLine();
}
sr.Close();
fs.Close();
//筛选
DataView dv = dt.DefaultView;
dv.RowFilter = " 日志内容 Like '%A%' and 日志时间 >= '2008-7-8 14:12:50' ";
//this.GridView1.DataSource = dt;
this.GridView1.DataSource = dv;
this.GridView1.DataBind();
}
//取得当前所应操作的日志文件的路径
private string GetLogFilePath()
{
string strFilePath = "";
string strYearMonth = DateTime.Now.ToString("yyyyMM");
string strLogDirPath = Server.MapPath("log");
//判断当前月份是否已有日志文件
string[] strFilesArray = Directory.GetFiles(strLogDirPath, "log_" + strYearMonth + "_*.txt");
if (strFilesArray.Length == 0)
{
strFilePath = Server.MapPath("log/log_" + strYearMonth + "_1.txt");
//之前没有本年月的日志文件 需要新建
using (File.Create(strFilePath))
{
}
}
else
{
int intOrderID = 1;
for (int i = 0; i < strFilesArray.Length; i++)
{
string strA = strFilesArray[i].Trim();
strA = strA.Substring(strA.LastIndexOf("_")+1);
strA = strA.Replace(".txt", "");
int intA = Convert.ToInt32(strA);
if (intA > intOrderID)
intOrderID = intA;
}
strFilePath = Server.MapPath("log/log_" + strYearMonth + "_" + intOrderID.ToString() + ".txt");
this.Label1.Text = strFilePath;
//之前有 需要判断最后一个是否超容
FileInfo fileInfo = new FileInfo(strFilePath);
if (fileInfo.Length >= 1024)
{
//超容了 新建之
int intCount = intOrderID + 1;
strFilePath = Server.MapPath("log/log_" + strYearMonth + "_" + intCount.ToString() + ".txt");
using (File.Create(strFilePath))
{
}
}
}
return strFilePath ;
}
讀寫ini文件
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,
string key,string val,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,
string key,string def, StringBuilder retVal,int size,string filePath);
public void IniWriteValue(string Section,string Key,string Value,string filePath)
{
WritePrivateProfileString(Section,Key,Value,filePath);
public string IniReadValue(string Section,string Key,string filePath)
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,
255, filePath);
return temp.ToString();
}
}
获取指定文件夹的大小
public long countsize( System.IO.DirectoryInfo dir)
{
long size=0;
FileInfo[] files=dir.GetFiles();
foreach(System.IO.FileInfo info in files)
{
size+=info.Length;
}
DirectoryInfo[] dirs=dir.GetDirectories();
foreach(DirectoryInfo dirinfo in dirs)
{
size+=countsize(dirinfo);
}
return size;
}
讀取資源檔:
ResourceManager _textResManager = new ResourceManager("testproject.MultiLanguage_Eng", Assembly.GetExecutingAssembly());
string resString = _textResManager.GetString("keyname");
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/High_Mount/archive/2009/08/28/4489655.aspx
C#读写日志文本文件的更多相关文章
- php分享二十六:读写日志
一:读写日志注意事项: 1:fgets取出日志行后,注意用trim过滤下 2:explode(“\t", $line) 拆分后,注意判断下个数是否正确,如果不正确,怎么处理? 如果某一列 ...
- IO流9 --- 使用FileInputStream和FileOutputStream读写非文本文件 --- 技术搬运工(尚硅谷)
字节流读写非文本文件(图片.视频等) @Test public void test5(){ File srcFile = new File("FLAMING MOUNTAIN.JPG&quo ...
- Java读写大文本文件(2GB以上)
如下的程序,将一个行数为fileLines的文本文件平均分为splitNum个小文本文件,其中换行符'r'是linux上的,windows的java换行符是'\r\n': package kddcup ...
- C文件读写(二进制/文本文件)整理
目录 [TOC] 打开文件 使用fopen打开文件,在<stdio.h>头文件中,其声明如下: FILE * fopen ( const char * filename, const ch ...
- Oracle写函数读写日志实例
1.用DBA登录赋权限create or replace directory D_OUTPUT as 'D:\TEMP'; grant read,write on directory D_OUTPUT ...
- Python读写txt文本文件
一.文件的打开和创建 ? 1 2 3 4 5 >>> f = open('/tmp/test.txt') >>> f.read() 'hello python!\n ...
- delphi读写文本文件
delphi读写文本文件 在工作中遇到了这样一个问题,使用PLSQL将一个表的数据转化成一些列的insert语句存储到一个.sql文本中,我本来想使用access数据库中的查询视图一次执行这些语句 ...
- [译]管理IIS日志的存储
原文:http://www.iis.net/learn/manage/provisioning-and-managing-iis/managing-iis-log-file-storage Overv ...
- java文件读写的两种方式
今天搞了下java文件的读写,自己也总结了一下,但是不全,只有两种方式,先直接看代码: public static void main(String[] args) throws IOExceptio ...
随机推荐
- Codeforces Round #339 (Div. 1) A. Peter and Snow Blower 计算几何
A. Peter and Snow Blower 题目连接: http://www.codeforces.com/contest/613/problem/A Description Peter got ...
- Codeforces Round #336 (Div. 2) D. Zuma 记忆化搜索
D. Zuma 题目连接: http://www.codeforces.com/contest/608/problem/D Description Genos recently installed t ...
- AES加密算法
代码是抄的,版权信息有 代码压缩包下载地址:http://pan.baidu.com/s/1jGEKH1c AES.h /////////////////////////////// // http: ...
- ACdream 1115 Salmon And Cat (找规律&&打表)
题目链接:传送门 题意: 一个数被觉得是一个完美的数,仅仅要须要满足下面的两个条件之中的一个 1)x = 1 or 3 2)x = 2 + a*b + 2*a + 2*b; a.b都是完美的数. 分析 ...
- iOS开发——UI篇Swift篇&玩转UItableView(三)分组功能
UItableView分组功能 class UITableViewControllerGroup: UIViewController, UITableViewDataSource, UITableVi ...
- Java中static、final用法小结
一.final 1.final变量: 当你在类中定义变量时,在其前面加上final关键字,那便是说,这个变量一旦被初始化便不可改变,这里不可改变的意思对基本类型来说是其值不可变,而对于对象变量来说其引 ...
- windows下如何github ssh 公钥
windows下如何github ssh 公钥 1. 安装git,从程序目录打开 "Git Bash" 2. 键入命令:ssh-keygen -t rsa -C " ...
- 简单的div元素拖拽到div
drag1 drag2 drag3 代码如下: <!DOCTYPE HTML> <html> <head> <title>div拖拽到div</t ...
- Docker大行其道—镜像
导读 作为Docker三大核心概念之一,Docker镜像(Docker Image)是一个面向Docker引擎的只读模板,包含文件系统.实际上每个Docker镜像包含一个独立的运行环境,如一个镜像包含 ...
- Google搜索技巧-从入门到精通(从此学习进步、工作顺心)
转载:http://www.blogbus.com/koudaizhi-logs/55687286.html 一 GOOGLE简介 Google (www.google.com)是一个搜寻引擎,由某 ...