我是C#的新手(不到一周),我有以下代码删除卷上的文件。它是SQL数据库的备份文件。并且它会不断增长,我必须重新命名该文件,然后在备份新文件后将其删除。我需要评估备份是否运行,然后可以删除该文件。
using System;
using System.IO;
class Program
{
static void Main()
{
//
// Delete a file found on the D:\ volume.
// If the file is not found (File doesn't exist),
// then you will get an exception.
//
try
{
File.Delete(@"\\Fabtrol-2\Program Files (x86)\FabTrolBackUp\FT_Trans_Log_Appendedold.BAK"); // Try to delete
Console.WriteLine("Deleted"); // Success
}
catch (IOException ex)
{
Console.WriteLine(ex); // Write error
}
}
}
这是逻辑。如果我问得太多,请告诉我:
答案 0 :(得分:3)
您可以通过以下方式大大简化逻辑: -
FileInfo info = new FileInfo(@"\\Fabtrol-2\Program Files (x86)\FabTrolBackUp\FT_Trans_Log_Appendedold.BAK");
if(DateTime.Now.AddDays(-2) > info.LastWriteTime.Date)
{
//Delete
}
答案 1 :(得分:1)
您需要做的就是遍历文件夹中的所有文件并删除超过两天的文件(正好是您的巨大条件声明)?
因此,请使用以下行获取文件夹中的所有文件:
string[] files = Directory.GetFiles(<folder name>);
然后,在循环中,遍历files
中的所有文件名并检查它们是否足够老。
要获取文件的时间戳,请使用FileInfo
和LastWriteTime
,如下所示:
if(DateTime.Today - someFileInfoObject.LastWriteTime.Date > new TimeSpan(2,0,0,0))
{
File.Delete();
}
答案 2 :(得分:0)
就个人经历一周的经历,我会说你已经跳到了最深处。
我不打算提供代码,而是指导您改进学习,这可以参考psuedo code
您可能需要查看单元测试,因为这可能有助于您的验证。
答案 3 :(得分:0)
在@Gray评论的基础上,FileInfo对象也会让你知道它是否.Exists
,减少了依赖try / catch来知道文件是否仍然存在的需要。
请考虑以下事项:
static void Main()
{
FileInfo fi = new FileInfo(@"\\path\\to\\file.bak");
if(fi.Exists)
{
try
{
//because we can still get errors based on permissions etc.
fi.Delete();
Console.WriteLine("Deleted"); // Success
}
catch (IOException ex)
{
Console.WriteLine(ex); // Write error
}
}
}
答案 4 :(得分:0)
尝试以下代码。
string backupFile = @"\\Fabtrol-2\Program Files (x86)\FabTrolBackUp\FT_Trans_Log_Appendedold.BAK";
FileInfo fi = new FileInfo(backupFile);
DateTime fileCreatedDate = File.GetCreationTime(backupFile);
DateTime today = DateTime.Now;
if (today.DayOfWeek != DayOfWeek.Monday && fileCreatedDate > today.AddDays(-2))
{
fi.Delete();
}
答案 5 :(得分:0)
此代码首先检查基于当天的文件是否少于2天,如果是,则删除文件FT_Trans_Log_Appendedold.BAK。
string dir = @"\\Fabtrol-2\Program Files (x86)\FabTrolBackUp\";
string fileName = dir;
switch (DateTime.Now.DayOfWeek)
{
case DayOfWeek.Monday:
fileName += "FT_FabTrol_Sun_Full.BAK";
break;
case DayOfWeek.Tuesday:
fileName += "FT_FabTrol_Mon_Full.BAK";
break;
case DayOfWeek.Wednesday:
fileName += "FT_FabTrol_Tues_Full.BAK";
break;
case DayOfWeek.Thursday:
fileName += "FT_FabTrol_Wed_Full.BAK";
break;
case DayOfWeek.Friday:
fileName += "FT_FabTrol_Thurs_Full.BAK";
break;
case DayOfWeek.Saturday:
fileName += "FT_FabTrol_Fri_Full.BAK";
break;
case DayOfWeek.Sunday:
fileName += "FT_FabTrol_Sat_Full.BAK";
break;
}
FileInfo fi = new FileInfo(fileName);
if (fi.Exists && DateTime.Now.AddDays(-2) > fi.LastWriteTime.Date)
{
(new FileInfo(dir + "FT_Trans_Log_Appendedold.BAK")).Delete();
Console.WriteLine("Deleted");
}
答案 6 :(得分:0)
更新了我的答案以满足您的要求。有点奇怪,但我很无聊。如果只是阅读可能会有所帮助。
//2 days
TimeSpan daysToKeep = new TimeSpan(2,0,0,0);
//The folder where the files are kept
DirectoryInfo backupFolder = new DirectoryInfo(@"\\Fabtrol-2\Program Files (x86)\FabTrolBackUp\");
//the Appendold.BAK file
FileInfo backupLog = new FileInfo(backupFolder.FullName + @"\FT_Trans_Log_Appendedold.BAK");
//the base name for the log files
string logName = "FT_FabTrol_{0}_Full.BAK";
//an array for the days of week that are part of the logname
string[] days = { "Sun", "Mon", "Tues", "Wed", "Thurs", "Fri", "Sat" };
//get yesterday's log file name
string yesterdayLog = String.Format(logName, days[(int)DateTime.Today.AddDays(-1).DayOfWeek]);
//create file info
FileInfo logFile = new FileInfo(backupFolder.FullName + yesterdayLog);
//if the file exists, and it is less than 2 days old
try
{
if (logFile.Exists && (DateTime.Today - logFile.LastWriteTime < daysToKeep))
{
backupLog.Delete();
Console.WriteLine("success");
}
else
{
Console.WriteLine("log file either did not exist or is not ready to delete");
}
}
catch(Exception ex)
{
Console.WriteLine(ex.Message);
}