自己写的操作记录的类,就是记录各种操作[原创]

原文发布时间为:2009-04-26 —— 来源于本人的百度文章 [由搬家工具导入]

网站的根目录下面创建一个文件夹 CaoZuoJiLu ,然后调用的时候 CaoZuo.JiLu("想记录的东西");

读取记录的时候:CaoZuo.ReadText(filepath);

    如下:

   if (!IsPostBack)
            {
                DropDownList1.Items.Insert(0, new ListItem("请选择要查看的记录", " "));

                string path = Server.MapPath("~/CaoZuoJiLu");
                DirectoryInfo di = new DirectoryInfo(path);
                foreach (FileInfo fi in di.GetFiles())
                {
                    ListItem li = new ListItem();
                    li.Value = fi.FullName;
                    li.Text = fi.Name;
                    DropDownList1.Items.Add(li);
                }
            }

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        //lbJilu.Text = File.ReadAllText(DropDownList1.SelectedValue,System.Text.Encoding.Default);
        lbJilu.Text = CaoZuo.ReadText(DropDownList1.SelectedValue.ToString());
    }

---------------------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.IO;
using System.Text;
/// <summary>
/// 各种操作的日志
/// </summary>
public class CaoZuo
{
    public CaoZuo()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    #region 操作记录
    public static void JiLu(string dosomething)
    {
        System.Collections.Specialized.NameValueCollection ServerVariables = System.Web.HttpContext.Current.Request.ServerVariables;
        string path = @"~/CaoZuoJiLu/";
        filePath = System.Web.HttpContext.Current.Server.MapPath(@"~/CaoZuoJiLu/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".txt");
        FileStream fs;
        StreamWriter sw;

        //判断文件否存在,不存在则创建
        if (!File.Exists(filePath))
        {
            fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);

        }
        else
        {
            fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
        }

        sw = new StreamWriter(fs, Encoding.Default);
        sw.WriteLine(DateTime.Now.ToLongTimeString() + " " + dosomething);
        sw.Close();
        fs.Close();
    }
    #endregion

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;


using System.IO;
using System.Text;
/// <summary>
/// 各种操作的日志
/// </summary>
public class CaoZuo
{
    public CaoZuo()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }
    #region 操作记录
    public static void JiLu(string dosomething)
    {
        System.Collections.Specialized.NameValueCollection ServerVariables = System.Web.HttpContext.Current.Request.ServerVariables;
        string filePath = null;
        filePath = System.Web.HttpContext.Current.Server.MapPath(@"~/CaoZuoJiLu/" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + ".txt");
        FileStream fs;
        StreamWriter sw;

        //判断文件否存在,不存在则创建
        if (!File.Exists(filePath))
        {
            fs = new FileStream(filePath, FileMode.Create, FileAccess.Write);

        }
        else
        {
            fs = new FileStream(filePath, FileMode.Append, FileAccess.Write);
        }

        sw = new StreamWriter(fs, Encoding.Default);
        sw.WriteLine(DateTime.Now.ToLongTimeString() + " " + dosomething);
        sw.Close();
        fs.Close();
    }
    #endregion

    #region 读取操作记录
    public static string ReadText(string fpath)
    {
        try
        {
            /**/
            ///从指定的目录以打开或者创建的形式读取日志文件
            FileStream fs = new FileStream(fpath, FileMode.Open, FileAccess.Read);
            /**/
            ///定义输出字符串
            StringBuilder output = new StringBuilder();

            /**/
            ///初始化该字符串的长度为0
            output.Length = 0;

            /**/
            ///为上面创建的文件流创建读取数据流
            StreamReader read = new StreamReader(fs, System.Text.Encoding.Default);

            /**/
            ///设置当前流的起始位置为文件流的起始点
            read.BaseStream.Seek(0, SeekOrigin.Begin);

            /**/
            ///读取文件
            while (read.Peek() > -1)
            {
                /**/
                ///取文件的一行内容并换行
                output.Append(read.ReadLine() + "<br/>");
            }

            /**/
            ///关闭释放读数据流
            read.Close();

            /**/
            ///返回读到的日志文件内容
            ///
            return output.ToString();
        }
        catch
        {
            //这两句是自己程序里面的
            throw new Exception("文件读取失败,请检查重要配置");
        }

    }
    #endregion
}

}

原文地址:https://www.cnblogs.com/handboy/p/7153319.html