【发布时间】:2013-09-11 19:23:10
【问题描述】:
我正在学习编写一个程序来删除服务器上超过指定日期的日志文件。该程序将基于表而不是基于服务器。使用 UNC,日志文件目录的路径存储在数据库中。我正在学习 Directory.GetFiles() 方法,但我不确定路径的语法是什么。我认为它将是 workList 因为它存储路径。任何解决此问题的帮助将不胜感激。我的代码如下。
//create list to store database contents
static List<LogData> GetWorkList()
{
List<LogData> logDatas = new List<LogData>();
LogData logData = new LogData();
//execute sql query
//execute database reader
string sqlQuery = "Select daysToKeep, fileLocation, active from dbo.FileDeletion where fileLocation='@fileLocation';";
SqlCommand command = new SqlCommand(sqlQuery);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//make a logData, creating a new instance of a class
logData = new LogData();
//move stuff from reader into log data, and exp
logData.DaysToKeep = Convert.ToInt32(reader["daysToKeep"]);
logData.Active = Convert.ToBoolean(reader["active"]);
logData.FileLocation = Convert.ToString(reader["fileLocation"]);
//add logData to list
logDatas.Add(logData);
}
return logDatas;
}
//Get active entries from table, call getWorkList
public static void WorkList(//Not sure of what path to use)
{
//get active entries
List<LogData> workList = GetWorkList();
foreach()
{
if(File.Exists()
{
}
}
//check to see if date created in directory is older that x number of days
if(DateTime.Now.Subtract(dt).TotalDays <= 1)
{
log.Info("This directory is less than a day old");
}
//if file is older than x number of days
else if (DateTime.Now.Subtract(dt).TotalDays <= //not sure of what variable or property to use)
{
File.Delete
}
//delete file
}
【问题讨论】:
-
您能举例说明
I'm not sure what the syntax would be the path的含义吗? (你有c:\temp\blah.log并且你想要c:\temp或blah.log) -
在数据库中名为“fileLocation”的列中列出了多个路径。我试图编写程序以将这些“多个”路径存储在列表中。该列表包含日志文件所在的所有路径的目录位置。因此,尝试使用硬编码的直接路径是行不通的,因为我希望它遍历列表并检查每个文件的 daysToKeep,如果文件早于 daysToKeep,则删除该文件。我希望这更清楚。我知道我想要什么,只是不知道如何到达那里
-
我遇到的问题是,没有关于
fileLocation中存储的内容的示例,您也没有给出您想要fileLocation值的哪一部分的示例。 -
\\gts101server\sharedFolder\Program Files\User\Services\Log Files\Logs 使用此路径,程序使用 UNC 转到列出的任何文件位置路径,无论它位于哪个服务器上。从而无需为每个服务器创建一个exe