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

 时间:2026-02-14 13:38:24

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

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

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

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

                {

                    Directory.CreateDirectory(dir);

                }

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

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、我们将上述代码放到一个方法中去,主要是为了方便调用,可以快速将要写入的内容写入到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、调用文件写入方法,将不同的内容写入到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文本文件

  • 明日方舟NL-S-1突袭低配攻略
  • 手机怎么保存和浏览gif动态图片
  • 阴阳师结界经验怎么收取
  • 怎么在微视给短视频表态
  • 苏宁易购申诉降级是什么意思,怎么弄
  • 热门搜索
    祖国在我心中手抄报内容 红十字会手抄报 抗击疫情手抄报内容 有关春天的手抄报 小学生植树节手抄报 元宵手抄报 秋天手抄报 爱牙手抄报 植树造林手抄报 六一儿童节手抄报简单又好看