C#添加txt文档和在文档内添加内容的方法

郝春妮

关注

阅读 84

2022-02-11

查询是否有文件存在,如果不存在则创建

string path = AppDomain.CurrentDomain.BaseDirectory + DateTime.Now.Date.ToString("yyyy-MM-dd") + ".txt";
            try
            {
                //判断文件是否存在,没有则创建。
                if (!System.IO.File.Exists(path))
                {
                    FileStream stream = System.IO.File.Create(path);
                    stream.Close();
                    stream.Dispose();
                }
            }
            catch
            {

            }

在文件中写入:

using (StreamWriter writer = new StreamWriter(path, true))
                    {
                        writer.WriteLine("123" + "     " + DateTime.Now.ToString() + DateTime.Now.ToString());
                    }

精彩评论(0)

0 0 举报