Alright, I'm at a loss here. I am trying to download a jar file and then make a batch file that runs it. I was able to download this file once before with my code, but now the completed event fires (I think, because the code inside it runs. Sorry I'm new-ish to C#.) instantly but no file is downloaded. I added a new button with nothing but the download code, and it the file was there, but it just showed up as 0KB. Nothing even shows up in Fiddler with my current code. I am using the IP address to skip the DNS checking and I set the proxy to null. (I read somewhere that doing this stops it from hanging. I think. It was something about web proxy auto-detection.) I can verify that the download links (which I don't show here, unless you need it) are real and downloads the file by simply opening it up in a web browser. Anyway, here is a snippet of my code:

        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);

    }
有帮助吗?

解决方案

wc1.DownloadFileAsync(new Uri(...), @txtboxFolder.Text + "\\jarfile.jar", @"c:\jarfile.jar"););
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top