日志为文本文件
每列以制表符隔开 行以换行符隔开

本次示例简单实现如下相关功能:
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#读写日志文本文件的更多相关文章

  1. php分享二十六:读写日志

    一:读写日志注意事项: 1:fgets取出日志行后,注意用trim过滤下 2:explode(“\t", $line) 拆分后,注意判断下个数是否正确,如果不正确,怎么处理?   如果某一列 ...

  2. IO流9 --- 使用FileInputStream和FileOutputStream读写非文本文件 --- 技术搬运工(尚硅谷)

    字节流读写非文本文件(图片.视频等) @Test public void test5(){ File srcFile = new File("FLAMING MOUNTAIN.JPG&quo ...

  3. Java读写大文本文件(2GB以上)

    如下的程序,将一个行数为fileLines的文本文件平均分为splitNum个小文本文件,其中换行符'r'是linux上的,windows的java换行符是'\r\n': package kddcup ...

  4. C文件读写(二进制/文本文件)整理

    目录 [TOC] 打开文件 使用fopen打开文件,在<stdio.h>头文件中,其声明如下: FILE * fopen ( const char * filename, const ch ...

  5. Oracle写函数读写日志实例

    1.用DBA登录赋权限create or replace directory D_OUTPUT as 'D:\TEMP'; grant read,write on directory D_OUTPUT ...

  6. Python读写txt文本文件

    一.文件的打开和创建 ? 1 2 3 4 5 >>> f = open('/tmp/test.txt') >>> f.read() 'hello python!\n ...

  7. delphi读写文本文件

    delphi读写文本文件   在工作中遇到了这样一个问题,使用PLSQL将一个表的数据转化成一些列的insert语句存储到一个.sql文本中,我本来想使用access数据库中的查询视图一次执行这些语句 ...

  8. [译]管理IIS日志的存储

    原文:http://www.iis.net/learn/manage/provisioning-and-managing-iis/managing-iis-log-file-storage Overv ...

  9. java文件读写的两种方式

    今天搞了下java文件的读写,自己也总结了一下,但是不全,只有两种方式,先直接看代码: public static void main(String[] args) throws IOExceptio ...

随机推荐

  1. jQuery + jQuery Mobile 实现省市二级下拉列表页面

    一.需求: 提供省.市下拉列表,当用户选择省一级下拉列表项后,市下拉列表的各个选项自动变为该省对应的城市列表. 二.效果: 三.实现: 1.省市json数据,来自: http://www.cnblog ...

  2. acdreamoj1108(The kth number)

    题目链接:http://acdream.info/problem? pid=1108 题意:n个数的数列,m次查询某个区间出现次数第k多的数出现的次数.n,m<=100000 解法:这个由于是离 ...

  3. data URI scheme

    优化网页效能,首要的任务是尽量减少HTTP请求(http request)的次数,例如把多个JavaScript文档合并,多个CSS文件合并等等.此外,还有有一种 data URL 的方法,可以直接把 ...

  4. KFC数据测试hbase结果

    两个field,一个是KFC数据 一个列放的内容是“same” 每条数据都flush   SLF4J: Failed to load class "org.slf4j.impl.Static ...

  5. jquery 动态事件绑定(0512)

    jquery动态事件绑定,父元素需为静态元素,(不能是动态生成): $("#parent").on("click","#child",fun ...

  6. LeetCode39 Combination Sum

    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C  ...

  7. IIS 之 托管管道模式

    IIS 7.0 支持两种管道模式: (1)IIS 7.0 最新提供的集成管道模式(Integrated), (2)经典管道模式(Classic),经典管道模式是由先前版本的IIS提供的. 我们可以通过 ...

  8. [JAVA] HTTP请求,返回响应内容,实例及应用

    JDK 中提供了一些对无状态协议请求(HTTP )的支持,下面我就将我所写的一个小例子(组件)进行描述: 首先让我们先构建一个请求类(HttpRequester ). 该类封装了 JAVA 实现简单请 ...

  9. MySQL(20):事务简介 和 事务的四个特性

    1. 事务概念引入: 现实生活中,我们往往经常会进行转账操作,转账操作可以分为两部分来完成,转入和转出.只有这两部分都完成了才可以认为是转账成功.在数据库中,这个过程是使用两条语句来完成的,如果其中任 ...

  10. iOS 项目审核被拒原因汇总

    1.程序运行崩溃 Your app crashes on iPhone running iOS connected to an IPv6 network when we: - app crashes ...