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

本次示例简单实现如下相关功能:
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. boost::token_compress_on

    对于场景:string s = "123456",用"3","4"切分,默认情况下(boost::token_compress_off),切 ...

  2. SQL 32位还原位64位出现3154错误

    1:首先检查新建数据库的路径是否正确. 2:勾选覆盖原有数据库. 3:新建查询,选择master.然后新建查询中进行如下直接对bak文件的操作. RESTORE DATABASE TestFROM D ...

  3. JS App

    JS App 从架构上来看, SPA结构-------RPA结构 不仅仅是JS,还要从Application的角度来看. JS只是单个页面或者功能,Application是从整体的角度来看结构.

  4. [Javascript] Promise-based functions should not throw exceptions

    Source You can also start a chain of then() method calls via Promise.resolve() and execute the synch ...

  5. Linux内核分析笔记 与Linux内核开发理论

    http://www.cnblogs.com/hanyan225/category/308793.html

  6. Blob未完成(待优化)

    /**************************************************/ /* Author : Anby */ /* Using :兼容powerBuilder中的B ...

  7. 如何保护你的linux操作系统

    如何保护你的linux操作系统 导读 在现在这个世道中,Linux操作系统的安全是十分重要的.但是,你得知道怎么干.一个简单反恶意程序软件是远远不够的,你需要采取其它措施来协同工作.那么试试下面这些手 ...

  8. 小白日记45:kali渗透测试之Web渗透-SqlMap自动注入(三)-sqlmap参数详解-Optimization,Injection,Detection,Techniques,Fingerprint

    sqlmap自动注入 Optimization [优化性能参数,可提高效率] -o:指定前三个参数(--predict-output.--keep-alive.--null-connection) - ...

  9. SolrEntityProcessor

    SolrEntityProcessor从不同的solr实例和内核中引入数据,这个数据是基于指定的或者是过滤的查询来获取到的.如果你需要复制索引,并且小幅度的修改目标索引文件中的数据,那么可以使用Sol ...

  10. 2015 FVDI V6.3 Software Free Download

    2015 FVDI with software USB dongle has newly upgraded to V6.3. Here software upgrade list: ABRITES C ...