1
I have a C# function that checks if a download has been completed by referring to whether the file exists .crdownload
is true.
If this does not exist, I assign a true boolean to the while variable, and theoretically I should close this loop. But it does not happen.
private bool CheckDownloadByPartialName_v2(string partialName, string path)
{
bool success = false;
DirectoryInfo directory = new DirectoryInfo(path);
while (!success)
{
bool downloading = directory.GetFiles()
.Where(file => file.Name.StartsWith(partialName))
.Where(file => file.Extension == ".crdownload")
.SingleOrDefault()
.Exists;
if (!downloading)
success = true;
}
return success;
}
Another problem I’m facing in this code is that when the file extension .crdownload
there is no (end of loop) my code from an Exception (Object Reference not set to an instance of an Object.)
Probably caused by the .SingleOrDefault()
of my LINQ query.
Someone can help me with these things?
It is absurd to do this, it will detonate the processor and disk for nothing, you have to use https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher?view=netframework-4.8.
– Maniero
I am aware that this can use a lot of processing, but it is just a small robot that visits the control panel of my VPN and downloads the access logs to an external directory.
– Hudson Medeiros
I can’t understand why the loop repeats after declaring the variable
sucess
astrue
– Hudson Medeiros