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 ...
随机推荐
- ReactNative学习实践--动画初探之加载动画
学习和实践react已经有一段时间了,在经历了从最初的彷徨到解决痛点时的兴奋,再到不断实践后遭遇问题时的苦闷,确实被这一种新的思维方式和开发模式所折服,react不是万能的,在很多场景下滥用反而会适得 ...
- Codeforces Round #146 (Div. 1) A. LCM Challenge 水题
A. LCM Challenge 题目连接: http://www.codeforces.com/contest/235/problem/A Description Some days ago, I ...
- C#窗体钉在桌面、置底、嵌入桌面的办法
想做一个桌面时钟,钉在桌面上不影响正常使用,只在看桌面的时候显示. 从网上多方寻找找到这么个代码,但是还是有不方便的地方,大家探讨一下. 这个程序在使用“显示桌面”的时候还可以显示,将程序的Form1 ...
- C语言(1+1+2+1+2+3....+n)
#include<stdio.h> void main(){ int i,j,a; long sum=0; //输入a的值 scanf("%d",&a); if ...
- 今天弱爆了,svn创建项目
今天弱爆了 1.再svnRoot下新建你要建的项目名如:hqdj 文件夹,然后选中它点击右键选中create repository here... ,选择文件系统类型 2.进入conf文件夹进行配置 ...
- 分享个新浪下载图片的ProgressBar进度样式
https://github.com/eltld/ImageLoadProgress2
- google使用技巧
- JDBC驱动的四种类型
Java中的JDBC驱动可以分为四种类型,包括JDBC-ODBC桥.本地API驱动.网络协议驱动和本地协议驱动. JDBC驱动类型一.JDBC-ODBC桥 JDBC-ODBC 桥 是sun公司提供的, ...
- page74-泛型可迭代的基础集合数据类型的API-Bag+Queue+Stack
[泛型可迭代的基础集合数据类型的API] 背包:就是一种不支持从中删除元素的集合数据类型——它的目的就是帮助用例收集元素并迭代遍历所有收集到的元素.(用例也可以检查背包是否为空, 或者获取背包中元素的 ...
- python 基础——变量
变量赋值 1. 把任意类型的变量[名称]赋值给新的变量,总是增加对象引用,而不是创建新的对象 2. 对于list.dict可变类型,操作的都是同一个对象 3. 使用del删除的是该对象的一个引用,只有 ...