C#创建目录,文件写入消息不覆盖原有消息

来源:互联网 发布:程序员的英文 编辑:程序博客网 时间:2024/05/16 17:00

  string toDir="F://"+DateTime.Now.ToString("yyyyMM");
             string errortext=DateTime.Now.ToString("//yyyyMMdd")+".txt";
            try
            {
                if (!Directory.Exists(toDir))
                {             
                    Directory.CreateDirectory(toDir);
                    FileStream fs1 = new FileStream(toDir + errortext, FileMode.Create);
                    StreamWriter sw = new StreamWriter(fs1);
                    sw.WriteLine("写入消息");
                    //清空缓冲区、关闭流
                      sw.Close();            
                   fs1.Close();
                }else{
                    StreamWriter sw = new StreamWriter(toDir + errortext, true);
                    sw.WriteLine("盗墓笔记");
                    sw.WriteLine("海贼王");
                    sw.WriteLine("秦时明月");
                    sw.Close();
                }
            }
            catch
            {
          //      return false;
            }
            finally
            {
                if (fos != null)
                {
                    try
                    {
                        fos.Close();
                    }
                    catch
                    { }
                }
            }
0 0
原创粉丝点击