c# - 下载文件并自动保存到文件夹

标签 c# file download save

我正在尝试制作一个用于从我的站点下载文件的用户界面。该站点有 zip 文件,这些文件需要下载到用户输入的目录中。但是,我无法成功下载该文件,它只是从一个临时文件夹中打开。

代码:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
        e.Cancel = true;
        string filepath = null;
            filepath = textBox1.Text;
            WebClient client = new WebClient();
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
            client.DownloadFileAsync(e.Url, filepath);
}

完整源代码: http://en.paidpaste.com/LqEmiQ

最佳答案

我的程序完全按照你的要求去做,没有提示什么的,请看下面的代码。

此代码将创建所有必要的目录(如果它们尚不存在):

Directory.CreateDirectory(C:\dir\dira\dirb);  // This code will create all of these directories  

此代码会将给定文件下载到给定目录(在上一个代码段创建之后:

private void install()
    {
        WebClient webClient = new WebClient();                                                          // Creates a webclient
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);                   // Uses the Event Handler to check whether the download is complete
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);  // Uses the Event Handler to check for progress made
        webClient.DownloadFileAsync(new Uri("http://www.com/newfile.zip"), @"C\newfile.zip");           // Defines the URL and destination directory for the downloaded file
    }

因此,使用这两段代码,您可以创建所有目录,然后告诉下载程序(不会提示您将文件下载到该位置。

关于c# - 下载文件并自动保存到文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6773866/

相关文章:

c# - 如何以 MHz 为单位快照处理器各个内核的速度

FILE I/O 中的 C 程序搜索

c# - JSON Newtonsoft C# 序列化/反序列化对象列表的良好实践

c - 尝试在 C 中读取迷宫文本文件时出现 malloc 错误

python - 如何在运行时更改目录并将文件存储在不同位置

linux - 使用Wget跳过成功下载的文件

css - Firefox 和 Safari 不会仅从我的服务器下载文件

asp.net - IE7 在重定向到大型 Excel 文件时出现问题

c# - mvc - 重用服务行为

c# - "private"和 "protected Internal"有什么区别?