创建日志文件

                string date = DateTime.Now.ToString("yyyy-MM-dd");
                string rootPath = System.Environment.CurrentDirectory;
                //string folderPath = rootPath + @"\Logs";
                string folderPath = @"D:\\Logs";
                string logPath = folderPath + "\\" + date + @".txt";
                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }
                if (!File.Exists(logPath))
                {
                    File.Create(logPath);
                }
                FileStream fileStream = new FileStream(logPath, FileMode.Append);
                StreamWriter writer = new StreamWriter(fileStream);
                string currentTime = DateTime.Now.ToString();
                writer.WriteLine("【" + currentTime + "】:" + str);
                writer.Dispose();
                writer.Close();
                fileStream.Dispose();
                fileStream.Close();

 

posted @ 2014-08-15 15:38  慧由心生  阅读(308)  评论(0编辑  收藏  举报