运行调度程序两次将使用控制台应用程序C#中的较早文件更新文件 [英] Running the scheduler twice is updating the file with the earlier file in Console application C#
问题描述
我有一个要求,我的调度程序一天要运行两次。早上一次,晚上第二次。当我运行当前代码时,它将文件存储在一个文件夹中。
I have a requirement where my scheduler will run twice in a day. One in the morning and a second time in evening. When I run my current code it stores the file in a folder.
因此,当我再次在晚上运行相同的应用程序时,发生的事情是保存了相同的文件一大早,我又不想发生这种情况了。我要保存两个文件。所以我该怎么做?
So again when I run the same application in the evening what happens is that the same file which is saved earlier in the morning is getting updated again which I don't want to happen. I want to save both the files. So what should I do?
下面是我当前的代码。请给我建议
Below is my current code. Please give me suggestions
public void ExportExcel(string strWorkbookName, DataSet ds)
{
string strDateFolder = "";
string strFileName = ConfigurationManager.AppSettings["FileName"].ToString();
try
{
using (XLWorkbook wb = new XLWorkbook())
{
strDateFolder = DateTime.Now.ToString("dd-MM-yyyy");
if (Directory.Exists(strDateFolder))
{
Directory.CreateDirectory(strDateFolder);
}
wb.Worksheets.Add(ds);
wb.SaveAs(ConfigurationRead.GetAppSetting("ReportDirectory") + "\\" + strDateFolder + "\\" + strFileName);
}
}
catch (Exception)
{
throw;
}
}
UPDATE
此外,我想删除7天后创建的文件夹。是否还可以?
Also, I want to delete the folder created after 7 days..Is that also possible ?
推荐答案
strDateFolder在两次运行中将包含相同的值,因为它获取日期。您可能需要增加时间,以便创建另一个文件。像这样:
strDateFolder will contain the same value through both runs because it gets the date. You may want to add time to that so it creates another file. Like this:
strDateFolder = DateTime.Now.ToString("dd-MM-yyyy-hh");
然后,下面的代码就像说:如果此目录存在,请创建它。
Then, the code below is like saying: if this directory exists, create it.
if (Directory.Exists(strDateFolder))
{
Directory.CreateDirectory(strDateFolder);
}
您只能使用它,因为它只会在不创建时创建它存在:
You can use only this, because it will create it only if it does not exist:
Directory.CreateDirectory(strDateFolder);
帖子更新:
这会删除较旧的文件夹那6天
Update from post: This would delete your folders older that 6 days
CultureInfo enUS = new CultureInfo("en-US");
string path = ConfigurationRead.GetAppSetting("ReportDirectory");
DateTime currentDate = DateTime.Now.AddDays(-7);
foreach (string s in Directory.GetDirectories(path))
{
string folderPath = s.Remove(0, path.Length);
if (DateTime.TryParseExact(folderPath, "dd-MM-yyyy hhmmss", enUS, DateTimeStyles.AssumeLocal, out DateTime td))
{
if (td <= currentDate)
{
Directory.Delete(s, true);
}
}
}
这篇关于运行调度程序两次将使用控制台应用程序C#中的较早文件更新文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!