I need help with C#, the question is how can I check if a .txt file exists in a folder using the timer counter, if the file already exists the counter will be stopped and attached. the message that the file is available, i ask the timer to check the folder continuously if the file already exists, the timer will stop, thanks
I hope someone will help
CodePudding user response:
That is my solution to your question.
DateTime DT_Start = DateTime.UtcNow;
bool FileIsFound = false;
const int ONESEC = 1000;
string Path = @"C:\Users\Schecher_1\Desktop";
string FileName = "test.txt";
while (!FileIsFound)
{
FileIsFound = Directory.EnumerateFiles(Path, FileName).Any();
Thread.Sleep(ONESEC);
}
Console.WriteLine($"The File {FileName} was found! "
$"{Environment.NewLine}"
$"{(DT_Start - DateTime.UtcNow).ToString("hh':'mm':'ss")} were needed!");
CodePudding user response:
var isFileExists = false;
while (!isFileExists)
{
isFileExists = Directory.EnumerateFiles("directoryToSearch", "*.txt").Any();
Thread.Sleep(1000);
}
Console.WriteLine("File found!");