【发布时间】:2016-10-26 07:09:14
【问题描述】:
我正在创建一个Visual C# application,它的部分功能是在.gz 文件出现在目录中时对其进行提取。一旦执行命令行参数,.gz 文件就会出现在指定目录中。
不幸的是,我收到一条错误消息,提示“找不到此文件”,这是因为它读取行以太快地提取 .gz 文件的原因。
换句话说,它试图在命令行参数执行之前执行.gz 文件并将文件实际放入目录中。
我想找到一种方法让我的程序在继续读取下一行之前等待文件出现在目录中。
以下是我的代码,任何帮助将不胜感激!谢谢!
else if (ddlDateType.Text == "Monthly" || ddlDateType.Text == "")
{
//Check if Monthly date entered is valid
if (DateTime.TryParseExact(txtDate.Text, MonthlyFormat, null,
System.Globalization.DateTimeStyles.None, out Test) != true)
{
MessageBox.Show("Enter a valid date.\nFormat: yyyyMM");
}
else
{
//Method that executes an arugment into the command prompt
ExecuteCommand();
//Method that extracts the file after it has already appeared in the directory
ExtractFile();
/*
Goal is to wait for the file to appear in the directory before it executes
the ExtractFile() method.
*/
}
}
【问题讨论】:
-
我的第一个想法就是做
while (!File.Exists(theFile)) { System.Threading.Thread.Sleep(1000); }
标签: c# directory file-exists visual-c#-express-2010