使用webclient在WPF应用程序中下载文件

时间:2015-08-20 15:59:23

标签: c# wpf webclient downloading

我正在制作WPF应用程序。 我正在使用WebClient下载文件。我有一个应该从当前路径下载的文件的名称列表。我使用foreach迭代每个名称,然后下载每个文件序列。我从我解码的torrent文件中获取的文件的名称。

public class DownloadGameFile
{
    private DownloadTorrentFile DLTorrent;

    //List of file that already exist
    private List<string> ExistFile = new List<string>();
    DirectoryInfo fileInfo;
    private volatile bool _completed;

    private string savePath = @"C:\Program Files (x86)\program\Client\package\downloads\";

    public DownloadGameFile()
    {
        DLTorrent = new DownloadTorrentFile();
        fileInfo = new DirectoryInfo(savePath);
    }
    public bool StartDownload(int torrentId)
    {
        try
        {
            DLTorrent.DecodeTorrent(torrentId);


            //File info from a Directory
            FileInfo[] files = fileInfo.GetFiles();

            foreach (FileInfo i in files)
            {
                Console.WriteLine("Files exit ");
                if (DLTorrent.GameInfomation[i.Name] != i.Length)
                {
                    i.Delete();
                }
                else
                {
                    Console.WriteLine("add files ");
                    ExistFile.Add(i.Name);
                }
            }
            //Make a list which file not downloaded yet
            var res = DLTorrent.GameInfomation.Keys.Except(ExistFile);

            foreach (var x in res)
            {
                Console.WriteLine(x);
            }

            foreach (var x in res)
            {
                DownloadProtocol("http://cdn.path.com/rental/" + torrentId + "/" + x, savePath + x);
            }

            return true;

        }
        catch
        {
            return false;
        }
    }

    public void DownloadProtocol(string address, string location)
    {
        WebClient client = new WebClient();
        Uri Uri = new Uri(address);

        client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);

        client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgress);
        client.DownloadFileAsync(Uri, location);


    }


    private void DownloadProgress(object sender, DownloadProgressChangedEventArgs e)
    {
        // Displays the operation identifier, and the transfer progress.
        Console.WriteLine("{0}    downloaded {1} of {2} bytes. {3} % complete...",
            (string)e.UserState,
            e.BytesReceived,
            e.TotalBytesToReceive,
            e.ProgressPercentage);
    }

    private void Completed(object sender, AsyncCompletedEventArgs e)
    {
        if (e.Cancelled == true)
        {
            Console.WriteLine("Download has been canceled.");
        }
        else
        {

            Console.WriteLine("Download completed!");
        }
    }

此代码在具有当前线程阻止程序的Console应用程序中正常工作。但是当我在WPF应用程序中使用相同的代码时却没有。我正在使用一个按钮来执行StartDownload()函数,但是当我这样做时,它会同时开始下载所有文件。示例第一个文件完成3%,然后切换到另一个文件,依此类推。我真的不知道为什么这不起作用。

1 个答案:

答案 0 :(得分:0)

您是否考虑过不使用.DownloadFileAsync?您可以尝试.DownloadFile并使用单个后台线程启动DownloadProtocol()。虽然我认为你必须重新考虑你的DownloadProgress输出。

我在winform中做了一些非常相似的事情。