0

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

    }
user1522456
  • 37
  • 1
  • 6

1 Answers1

2
wc1.DownloadFileAsync(new Uri(...), @txtboxFolder.Text + "\\jarfile.jar", @"c:\jarfile.jar"););
Hassan Boutougha
  • 3,871
  • 1
  • 17
  • 17
  • Ok, that seems to be what I want. My code is doing the wrong filename and what not, but I can sort that out. But just a quick question, HOW does that make it work? EDIT: Ok, so my code to choose what download link doesn't work so it always downloaded the last file, but by doing what you just did, it miraculously worked. And now I took it away, and it STILL works. Thanks anyway. – user1522456 Aug 17 '12 at 22:47
  • it downloads jarfile.jar in your local computer http://msdn.microsoft.com/en-us/library/ms144197.aspx – Hassan Boutougha Aug 17 '12 at 22:52
  • OH WOW. I'm stupid. I was setting the file name and not actually putting it anywhere. And the code I was using for the last download was messed up before I posted it here. Thanks so much. – user1522456 Aug 17 '12 at 22:57
  • you are welcome, it often happen to have some dismiss the most important is to be persevering my friend ;-) – Hassan Boutougha Aug 17 '12 at 23:03