简体   繁体   English

等待文件,直到使用 C# 下载它

[英]Waiting for a file until it is downloaded using C#

I'm using C# to download different files via url and run them afterwards from a physical filepath in the system, however I need to wait for the file until it is completely written and only then run it.我正在使用 C# 通过 url 下载不同的文件,然后从系统中的物理文件路径运行它们,但是我需要等待文件直到它被完全写入然后才运行它。 The reason is that some files would need more time than others to be saved and I can't really use Thread.Sleep() for this purpose.原因是某些文件需要比其他文件更多的时间来保存,我不能真正使用 Thread.Sleep() 来达到这个目的。

I tried this code but it is not that flexible for some reason, as I can't really tell how many tries or how much time it should be until the file is saved.我尝试了这段代码,但由于某种原因它不是那么灵活,因为我无法真正说出在保存文件之前应该尝试多少次或需要多长时间。 This depends always on the internet connection as well as on the file size.这始终取决于互联网连接以及文件大小。

   WebClient client = new WebClient();
   var downloadTask = client.DownloadFileTaskAsync(new Uri(url), filepath);
   var checkFile = Task.Run(async () => await downloadTask);
   WaitForFile(filepath, FileMode.CreateNew);

    FileStream WaitForFile(string fullPath, FileMode mode)
    {
        for (int numTries = 0; numTries < 15; numTries++)
        {
            FileStream fs = null;
                try
                {
                    fs = new FileStream(fullPath, mode);
                    return fs;
                }

                catch (IOException)
                {
                    if (fs != null)
                    {
                        fs.Dispose();
                    }
                    Thread.Sleep(50);
                }
        }

        return null;
    }

Is there a way to keep waiting until the File.Length > 0?有没有办法一直等到 File.Length > 0?

I would appreciate any help.我将不胜感激任何帮助。

Thanks.谢谢。

You're not awaiting for the file to complete download.您无需等待文件完成下载。 Or better said, you're awaiting, in a different thread, and then throwing that result away.或者更好地说,你正在等待,在不同的线程中,然后把结果扔掉。 Just wait in the very same method and you no longer need a separate way to know when the file is downloaded只需以相同的方法等待,您不再需要单独的方法来知道文件何时下载

WebClient client = new WebClient();
await client.DownloadFileTaskAsync(new Uri(url), filepath);

You can use FileSystemWatcher to get an event when file changes its size or a new file appear https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher.onchanged?view=netcore-3.1当文件更改其大小或出现新文件时,您可以使用 FileSystemWatcher 获取事件https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher.onchanged?view=netcore-3.1

But you can't really tell if the file is fully downloaded this way unless you know its size.但是除非您知道文件的大小,否则您无法真正判断文件是否以这种方式完全下载。

You should just change the code that downloads the file so that it notifies when the file is downloaded.您应该只更改下载文件的代码,以便在下载文件时发出通知。

I think this is all you have to do:我认为这就是你所要做的:

WebClient client = new WebClient();
 await client.DownloadFileTaskAsync(new Uri(url), filepath);
 // code continues when the file finishes downloading

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM