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

本次示例简单实现如下相关功能:
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");

C#读写日志文本文件相关推荐

  1. ASP 如何读写一个文本文件

    利用Active Server Pages脚本,你就几乎完全可以控制服务器的文件系统.需要如下的组件: ■FileSystemObject:这个对象包括了一些基本的对文件系统进行操作的方法,比如说,复 ...

  2. Android10.0 日志系统分析(三)-logd、logcat读写日志源码分析-[Android取经之路]

    摘要:本节主要来讲解Android10.0 logd.logcat读写日志源码内容 阅读本文大约需要花费20分钟. 文章首发微信公众号:IngresGe 专注于Android系统级源码分析,Andro ...

  3. python读取txt文件写入-Python读写txt文本文件的操作方法全解析

    一.文件的打开和创建 >>> f = open('/tmp/test.txt') >>> f.read() 'hello python! hello world! ...

  4. VB 读写TXT文本文件函数

    以下两个函数分别读取TXT文本文件与写入文件 Private Function GetTXT(Path As String) '读取文本     Open Path For Input As #1   ...

  5. Python读写txt文本文件的操作方法全解析

    这篇文章主要介绍了Python读写txt文本文件的操作方法全解析,包括对文本的查找和替换等技巧的讲解,需要的朋友可以参考下 一.文件的打开和创建 ? 1 2 3 4 5 >>> f ...

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

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

  7. 2012文件服务器 读写日志,管理用户访问日志记录记录

    管理用户访问日志记录记录 10/16/2017 本文内容 适用范围:Windows Server 2022.Windows Server 2019.Windows Server 2016.Window ...

  8. python关闭读写的所有的文件-Python读写txt文本文件的操作方法全解析

    一.文件的打开和创建 >>> f = open('/tmp/test.txt') >>> f.read() 'hello python! hello world! ...

  9. python读取整个txt文件-Python读写txt文本文件

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

  10. python读文本文件的过程是怎样的_读写文本文件的步骤_Python读写txt文本文件的操作方法全解析...

    一.文件的打开和创建 >>> f=open('/tmp/test.txt') >>> f.read() 'hello python!hello world!' &g ...

最新文章

  1. PCL中的OpenNI点云获取框架(OpenNI Grabber Framework in PCL)
  2. 图像检测技术的研究现状
  3. 学号20145332 《信息安全系统设计基础》实验四 驱动程序设计
  4. ftp获取远程Pdf文件
  5. 决策树有框架吗_决策框架
  6. 《Java技术》第一次作业
  7. 树莓派 python_树莓派笔记08—Python流水灯
  8. virtual box linux 安装增强功能,Linux 开发环境中为VirtualBox安装增强功能
  9. 页面的title为乱码的话需要修改jsp页面pageEncoding=UTF-8
  10. IDEA高级玩法:集成JIRA、UML类图插件、SSH、FTP、Database管理...
  11. java wait() notify_Java的wait(), notify()和notifyAll()使用小结
  12. MySql表空间的概念
  13. 明小子动力上传拿webshell.zip
  14. Excel冻结窗口及设置下拉菜单
  15. /专访/对话堵俊平:最好的开源生态模型,是亚马逊的原始森林
  16. 02-Sentinel-2 L1C级数据bat和Python脚本批量大气校正
  17. 阿里云搭建大数据平台(9):kafka安装部署和测试
  18. 这样处理,Java中的注释代码也会执行
  19. thinkphp项目_简历网站
  20. 安全工程转计算机专业,2019安全工程专业就业前景和就业方向分析

热门文章

  1. 【图像边缘检测】基于matlab CNN灰度图像边缘提取【含Matlab源码 490期】
  2. 【雷达通信】基于matlab GUI雷达脉冲压缩【含Matlab源码 303期】
  3. 从零开始带你成为消息中间件实战高手_系统化开课了,电子入门一切从零开始,喜欢电子的有机会学习了...
  4. 学习遗忘曲线_级联相关,被遗忘的学习架构
  5. 计算机电缆静电,ZR-DJFPVP计算机电缆
  6. 计算机系统结构自考知识点总结,自考《计算机系统结构》问答题总结(3)
  7. python的cubes怎么使用_Python之Cubes框架使用
  8. 前端怎么做接口签名验证 -baijiahao_阿里一面:如何保证API接口数据安全?
  9. python在工作中怎么用_在Python中调用是如何工作的?
  10. 查看mysql 二进制日志_Mysql主从复制原理及搭建