调用自动下载URL而不打开浏览器

时间:2018-07-06 09:02:49

标签: c# file download windows-forms-designer

下载文件时遇到小问题。
我使用过Windows Forms,需要下载几个文件。它们都可以通过链接打开,该链接在打开时会自动下载文件。

我尝试了WebCient或httpWebrequest之类的几种方法,但是都无法正常工作。

您是否知道如何在不每次都打开浏览器的情况下启动此链接并将文件安全保存到特定文件夹。

                    foreach (var doc in newDocs)
                {
                    using (var wb = new WebClient())
                    {
                        MessageBox.Show("link" <- to cehck the if its the correct file;
                        HttpWebRequest request = WebRequest.Create("link") as HttpWebRequest;

                        wb.DownloadFile("link" <- 'link' cause its sensitive data.);
                    }
                }

1 个答案:

答案 0 :(得分:0)

希望它会对您有所帮助。

 void Download()
    {
        using (WebClient client = new WebClient())
        {
            client.DownloadProgressChanged += Client_DownloadProgressChanged;
            client.DownloadFileCompleted += Client_DownloadFileCompleted;
            client.DownloadFileAsync(new Uri("url"), @"filepath");
        }
    }

    private  void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        //throw new NotImplementedException();
    }

    private  void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        //throw new NotImplementedException();
    }