读写日志文件

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

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

转载于:https://www.cnblogs.com/jearay/p/3668975.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/575504.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

游戏策划试题(1)——摘自牛客网

对于游戏涉及的不多&#xff0c;但是对暴雪在各种经验贴和小道消息上还是有些了解的。涉猎一下 策划游戏之类 的面试知识&#xff0c;横向拓宽知识面&#xff0c;也有助于自己拓宽视野&#xff0c;看看这种游戏文案策划 作为一名运营策划&#xff0c;你觉得可通过哪些途径起到保…

Android Fragment应用实战

现在Fragment的应用真的是越来越广泛了&#xff0c;之前Android在3.0版本加入Fragment的时候&#xff0c;主要是为了解决Android Pad屏幕比较大&#xff0c;空间不能充分利用的问题&#xff0c;但现在即使只是在手机上&#xff0c;也有很多的场景可以运用到Fragment了&#xff…

初识树莓派

初识树莓派这一微型计算机&#xff0c;基本了解概念&#xff0c; 1、树莓派简介 Raspberry Pi(中文名为“树莓派”,简写为RPi&#xff0c;或者 RasPi/RPi)是为学生计算机编程教育而设计&#xff0c;只有信用卡大小的卡片式电脑&#xff0c;其系统基于Linux开发而来的。树莓派由…

ffmpeg抓取rtsp流并保存_详细解析RTSP框架和数据包分析(1)

0.引言 本文主要讲解RTSP框架和抓取RTSP数据包&#xff0c;进行详细分析。可以阅读以下几篇文章&#xff0c;能够帮助你更详细理解。 手把手搭建RTSP流媒体服务器 HLS实战之Wireshark抓包分析 HTTP实战之Wireshark抓包分析 1.RTSP协议简述 RTSP&#xff1a;Real Time Stream…

遍历窗体中所有控件的信息

public void TraverControl(Control Ctl) {   foreach (Control c in Ctl.Controls)  {     label1.Text "\n" "" c.Name "" "\n";     //用于显示窗体中包含的所有的控件名&#xff0c;首先显示的是最外层的控件  …

xshell 软件的窗口一直是置顶 调整为不置顶

突然感觉xshell 软件的窗口一直是置顶状态&#xff0c;点别的软件窗口点不过去&#xff0c; 搜索一下&#xff1a;微信的 alta是截图&#xff0c;和xshell这个冲突了。如果在xshell下按了这个&#xff0c;不知不觉就被置顶了。。。。。MMP 即这个意思&#xff1a; 如果要修改…

Linux下Vim工具常用命令

原文地址&#xff1a; http://www.cnblogs.com/lizhenghn/p/3675011.html 在linux下做开发&#xff0c;甚至是只做管理维护工作&#xff0c;也少不了Vim的使用。作为一个新手&#xff0c;我也是刚刚接触&#xff0c;本节将我日常使用或收集的Vim常用命令记录下来。 当然&#x…

整理加解释:以太网、快速以太网、千兆以太网和万兆以太网分别的概念和区分 大详解

一、以太网是什么 以太网(Ethernet)最早是由Xerox(施乐)公司创建的局域网组网规范&#xff0c;1980年DEC、Intel和Xeox三家公司联合开发了初版Ethernet规范—DIX 1.0&#xff0c;1982年这三家公司又推出了修改版本DIX 2.0&#xff0c;并将其提交给EEE 802工作组&#xff0c;经I…

zoj 1109 Language of FatMouse 解题报告

题目链接&#xff1a;http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId109 题目意思&#xff1a;给出一个mouse-english词典&#xff0c;问对于输入的mouse单词&#xff0c;能否在这个词典里找出对应的english&#xff0c;不能输出“eh” 这里用到map来做&#xf…

测试telnet安装成功 以及如何进入和退出telnet命令模式

telnet装好之后&#xff0c;测试。 输入&#xff1a;telnet www.baidu.com 80 出现如图提示是意思是&#xff1a; 正常连通。 然后 按 ctrl ] 进入telnet客户端命令模式&#xff0c;然后用quit退出。进入linux命令模式。

数据按时间拆开分批处理示例

我现在的问题是有一个大的事实表,已经有数十亿条数据,过来的临时表需要merge进去. 临时表的大小也不确定,可能上十亿也可能只有几百几千万而已. 如果直接让这两个表merge起来,则需要很大的内存来进行处理.所以我就想着把数据进行按时间拆分的处理,然后merge进去, 拆分的条件是如…

Linux E325: ATTENTION Found a swap file by the name “./.backu.sh.swp“

在vi编辑时遇到: E325: ATTENTION Found a swap file by the name "./.backu.sh.swp"错误代码。 原因是上次编辑的时候&#xff0c;卡了或者别的原因ctrl c强制退出了。下次进来Linux还是默认进入这个上次强制退出的文件&#xff0c;所以报错。 解决方…

python 函数的参数对应

作者&#xff1a;Vamei 出处&#xff1a;http://www.cnblogs.com/vamei 欢迎转载&#xff0c;也请保留这段声明。谢谢&#xff01; 我们已经接触过函数(function)的参数(arguments)传递。当时我们根据位置&#xff0c;传递对应的参数。我们将接触更多的参数传递方式。 回忆一下…

telnet远程登陆、mstsc远程控制、SSH之间的比较和区别

Telnet与远程桌面mstsc 一 演示 打开mstsc演示&#xff1a; 01 win r 输入mstsc 02 输入要远程的机子的用户名和密码 03 登录 04 连接开启了&#xff0c;如图即是通过win窗口操作远程机子。 以上是使用mstsc打开一个远程连接来通过windows窗口的方式来控制远程机器。 …

c语言中'.'与'-'的区别

记录学习c语言中遇见的问题。定义的结构体如果是指针&#xff0c;访问成员时就用->如果定义的是结构体变量&#xff0c;访问成员时就用.例如&#xff1a;struct AAA { int a; char b;};struct AAA q; 访问成员就用&#xff1a;q.a;struct AAA *p; 访问成员就用&#x…

Linux查看已经开放的端口,开放端口遇到防火墙的问题。

Linux查看已经开放的端口&#xff0c;开放端口&#xff0c;FirewallD is not running 查看已经开放的端口 firewall-cmd --list-all1 如果出现这种情况说明你的防火墙没有开: 则先把防火墙打开&#xff1a;systemctl start firewalld 扩展内容&#xff1a;. 查看firewalld状…

内核态(Kernel Mode)与用户态(User Mode)

内核态: CPU可以访问内存所有数据, 包括外围设备, 例如硬盘, 网卡. CPU也可以将自己从一个程序切换到另一个程序 用户态: 只能受限的访问内存, 且不允许访问外围设备. 占用CPU的能力被剥夺, CPU资源可以被其他程序获取 为什么要有用户态和内核态 由于需要限制不同的程序之间的访…

用yum装程序 报[Errno 12] Timeout on Trying other mirror.

使用yum&#xff0c;报错如图下&#xff1a; 记着之前网卡和dns文件都配置好着的&#xff0c;114.114.114.114都加上的。 检查了下&#xff0c;发现dns文件的配置不见了。。。。 故把 /etc/resolv.conf 中加了如下&#xff1a;然后重点不要重启 service network restart &#…

JavaScript中的原型和继承

请在此暂时忘记之前学到的面向对象的一切知识。这里只需要考虑赛车的情况。是的&#xff0c;就是赛车。 最近我正在观看 24 Hours of Le Mans &#xff0c;这是法国流行的一项赛事。最快的车被称为 Le Mans 原型车。这些车虽然是由“奥迪”或“标致”这些厂商制造的&#xff0c…

Centos7.x 网卡启动报错(Failed to start LSB: Bring up/down networking)

环境&#xff1a;华为云服务器、 CentOS 7.x 操作是 调整网卡配置文件和resolv.conf &#xff0c;systemctl restart network 重启网络服务总是失败&#xff1a;如下&#xff1a; 按提示看细节&#xff1a; 搜索到还算靠谱的帖子,都说 大多都是网卡配置文件配置错误&#x…