欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

Asp.net怎样将一个数据写入到.txt txt 文件 log 日志文件 有大用 有大大用

很多时候我们会将一些信息通过程序将信息记录到txt文本中,那么如何使用asp.net将数据写入到txt中,下面,小编为小伙伴讲解一下。

工具/原料

  • vs2015

方法/步骤

  1. 1

    对于.net写入txt的操作,实际过程就是一个文件写入的过程。我们会使用到Directory、FileInfo等等一些基本的文件操作类。下面,小编以实例来介绍如何将数据写入到txt中。

  2. 2

    编写写入txt的方法,首选,要判断txt所在的文件夹是否存在,以防程序读取不到目录出现异常,如果不存在,我们通过程序来创建文件夹。

     string dir = System.Web.HttpContext.Current.Server.MapPath("~/log");

                    if (Directory.Exists(dir) == false)

                    {

                        Directory.CreateDirectory(dir);

                    }

    Asp.net怎样将一个数据写入到.txt文本文件                    
  3. 3

    判断txt文件是否存在,如果不存在,先要创建文件,否在直接打开文件,将内容追加进去。

     string strFilePath = System.Web.HttpContext.Current.Server.MapPath("~/log/log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt");

                    FileInfo logFile = new FileInfo(strFilePath);

                    System.IO.FileStream fs;

                    if (logFile.Exists)

                    {

                        fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append);

                    }

                    else

                    {

                        fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Create);

                    }

                    System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);

                    sw.WriteLine("---------------------------------------------------------------------------------------");

                    sw.WriteLine("-----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "---------------------------------------");

                    sw.WriteLine("---------------------------------------------------------------------------------------");

                    sw.WriteLine(logStr);

                    sw.Close();

                    fs.Close();

    Asp.net怎样将一个数据写入到.txt文本文件                    
  4. 4

    我们将上述代码放到一个方法中去,主要是为了方便调用,可以快速将要写入的内容写入到txt中。

     public static void CreateWebLog(string logStr)

            {

                try

                {


                       

     


                       

                    string dir = System.Web.HttpContext.Current.Server.MapPath("~/log");

                    if (Directory.Exists(dir) == false)

                    {

                        Directory.CreateDirectory(dir);

                    }

                    string strFilePath = System.Web.HttpContext.Current.Server.MapPath("~/log/log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt");

                    FileInfo logFile = new FileInfo(strFilePath);

                    System.IO.FileStream fs;

                    if (logFile.Exists)

                    {

                        fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Append);

                    }

                    else

                    {

                        fs = new System.IO.FileStream(strFilePath, System.IO.FileMode.Create);

                    }

                    System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.Default);

                    sw.WriteLine("---------------------------------------------------------------------------------------");

                    sw.WriteLine("-----------------------------" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "---------------------------------------");

                    sw.WriteLine("---------------------------------------------------------------------------------------");

                    sw.WriteLine(logStr);

                    sw.Close();

                    fs.Close();

                }

                catch (Exception)

                {

                }

            }

    Asp.net怎样将一个数据写入到.txt文本文件                    
  5. 5

    调用文件写入方法,将不同的内容写入到txt中。这里要分情况来写入,如果是字符串类型的内容,可以直接调用方法,如果是其他类型,则需要先转换类型才能写入。最后,写入到txt的内容如图:

     Log.CreateWebLog("我是小编");//因为定义的方法是静态方法,可以直接调用

    Log.CreateWebLog("GetPagedList 方法中的异常报错=" + ex.ToString());

    Log.CreateWebLog("GetPagedList 方法中的异常报错=" + ex.ToString());

     Log.CreateWebLog(JSONHelper.ToJson(messageList));//list类型的要转成json字符串

    Asp.net怎样将一个数据写入到.txt文本文件                    
    Asp.net怎样将一个数据写入到.txt文本文件                    
    END                
经验内容仅供参考,如果您需解决具体问题(尤其法律、医学等领域),建议您详细咨询相关领域专业人士。
作者声明:本篇经验系本人依照真实经历原创,未经许可,谢绝转载。

来自   https://jingyan.baidu.com/article/9f63fb91e5906788400f0ebf.html


普通分类: