WebClient出现问题

时间:2015-11-11 15:09:54

标签: c#

我试图为RADAR图像等制作图像存档(r).WebClient似乎与我有问题。或者我不知道自己在做什么。

WebClient wc = new WebClient();
{
     wc.Headers.Add("User-Agent: Other");
     wc.DownloadFile("http://www.weather.gov/images/dlh/WxStory/FileL.png?", browserFileDialog.SelectedPath() + ".png");
}

每当我下载文件时,我都有一个浏览文件对话框。它将保存到桌面,并获取在"浏览文件对话框中选择的文件夹的名称"。任何帮助将不胜感激。

编辑:我忘了说,我希望用户能够选择保存位置。

1 个答案:

答案 0 :(得分:0)

您可以使用SaveFileDialog获取所需的路径:

using (SaveFileDialog sfd = new SaveFileDialog())
{
    // This only allows you to choose PNG, you may want to change it.
    sfd.Filter = "Image Files (*.png)|*.png";

    DialogResult result = sfd.ShowDialog();

    if (result == DialogResult.OK)
    {
        using (WebClient wc = new WebClient())
        {
            wc.Headers.Add("User-Agent: Other");
            wc.DownloadFile("http://www.weather.gov/images/dlh/WxStory/FileL.png?", sfd.FileName);
         }
     }
 }

还有一些可能有用的SaveFileDialog参数,我为了简洁而错过了这些参数。我也没有在这里放错处理,所以你也需要这样做。

相关问题