DownloadFileAsync不下载文件但已完成事件被触发?

时间:2012-08-17 22:35:12

标签: c# webclient webclient-download downloadfileasync

好吧,我在这里不知所措。我试图下载一个jar文件,然后制作一个运行它的批处理文件。我之前能够使用我的代码下载此文件,但现在已经完成的事件触发了(我想,因为它里面的代码运行。抱歉我是C#的新手。)但是没有文件下载。我添加了一个新的按钮,只有下载代码,文件就在那里,但它只显示为0KB。使用我当前的代码,甚至没有任何东西出现在Fiddler中。我使用IP地址跳过DNS检查,我将代理设置为null。 (我在某处读到这样做会阻止它挂起。我想。这是关于Web代理自动检测的东西。)我可以验证下载链接(我没有在这里显示,除非你需要它)是真实的只需在Web浏览器中打开即可下载文件。无论如何,这是我的代码片段:

        WebClient wc1 = new WebClient();
        wc1.DownloadFileCompleted += new AsyncCompletedEventHandler(wc1_DownloadFileCompleted);
        wc1.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wc1_DownloadProgressChanged);

        if (cmboboxVersion.SelectedText == ...)
        {
            stsprgsbar.Style = ProgressBarStyle.Continuous;

            stslblStaus.Text = "Downloading files...";

            wc1.DownloadFileAsync(new Uri(...), @txtboxFolder.Text + "\\jarfile.jar");

            FileStream fs = new FileStream(@txtboxFolder.Text + "\\batfile.bat", FileMode.Create, FileAccess.ReadWrite);

            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(...);
            sw.Close();

            fs.Close();


        }
        else if (cmboboxVersion.SelectedText == ...)
        {
            stsprgsbar.Style = ProgressBarStyle.Continuous;

            stslblStaus.Text = "Downloading files...";

            wc1.DownloadFileAsync(new Uri(...), @txtboxFolder.Text + "\\jarfile.jar");

            FileStream fs = new FileStream(@txtboxFolder.Text + "\\batfile.bat", FileMode.Create, FileAccess.ReadWrite);

            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(...);
            sw.Close();

            fs.Close();
        }
        else
        {
            stsprgsbar.Style = ProgressBarStyle.Continuous;

            stslblStaus.Text = "Downloading files...";

            wc1.DownloadFileAsync(new Uri(...), @txtboxFolder.Text + "\\jarfile.jar");

            FileStream fs = new FileStream(@txtboxFolder.Text + "\\batfile.bat", FileMode.Create, FileAccess.ReadWrite);

            StreamWriter sw = new StreamWriter(fs);
            sw.WriteLine(...);
            sw.Close();

            fs.Close();
        }
    }

    public void wc1_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        stsprgsbar.Value = e.ProgressPercentage;
    }

    public void wc1_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
    {
        string BatPath = @txtboxFolder.Text + "\\batfile.bat";
        stsprgsbar.Style = ProgressBarStyle.Marquee;
        stslblStaus.Text = "Generating files...";
        ProcessStartInfo pro = new ProcessStartInfo(BatPath);
        //pro.CreateNoWindow = true;
        Process.Start(pro);

    }

1 个答案:

答案 0 :(得分:2)

wc1.DownloadFileAsync(new Uri(...), @txtboxFolder.Text + "\\jarfile.jar", @"c:\jarfile.jar"););