0
点赞
收藏
分享

微信扫一扫

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

郝春妮 2022-02-11 阅读 84
c#

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

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 条评论