删除文件夹下30天之前的文件,并指定文件类型
/// <summary> /// 删除文件夹及其子文件夹内过期文件 /// </summary> /// <param name="strFolderPath"></param> public void DelFile(string strFolderPath) { if (System.IO.Directory.Exists(strFolderPath)) { DirectoryInfo dyInfo = new DirectoryInfo(strFolderPath); foreach (FileSystemInfo feInfo in dyInfo.GetFileSystemInfos()) { if (System.IO.Directory.Exists(feInfo.FullName)) { DelFile(feInfo.FullName); } //判断文件日期是否小于今天,是则删除 if (feInfo.CreationTime < DateTime.Now.AddDays(-30)) { if (feInfo.Extension == ".txt") { feInfo.Delete(); } } } } }
//获取文件夹下所有的文件
string strFolderPath = System.AppDomain.CurrentDomain.BaseDirectory + "Logs\\error\\"; DirectoryInfo dyInfo = new DirectoryInfo(strFolderPath); //获取文件夹下所有的文件 foreach (FileInfo feInfo in dyInfo.GetFiles()) { //判断文件日期是否小于今天,是则删除 if (feInfo.CreationTime < DateTime.Now.AddDays(-30)) { if (feInfo.Extension == ".txt") { feInfo.Delete(); } } }
//清空文本内容 FileStream stream = File.Open(path, FileMode.OpenOrCreate, FileAccess.Write); stream.Seek(0, SeekOrigin.Begin); stream.SetLength(0); stream.Close(); //覆盖或追加文本内容 StreamWriter sw = new StreamWriter(path, false);//false表示覆盖true是追加 sw.WriteLine("afsgdfgafsgsss"); sw.Close();