读写日志文件
日志為文本文件
每列以制表符隔開 行以換行符隔開
本次示例簡單實現(xiàn)如下相關功能:
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
總結
- 上一篇: for语句中声明变量
- 下一篇: IOS_OC_id ,NSObject,