好吧,我在这里不知所措。我正在尝试下载一个jar文件,然后制作一个运行它的批处理文件。我以前可以用我的代码下载这个文件一次,但现在完成的事件立即启动(我想,因为里面的代码在运行。对不起,我是C#的新手。)但没有下载任何文件。我添加了一个新按钮,除了下载代码什么都没有,文件就在那里,但它只是显示为0KB。Fiddler中甚至没有显示我当前的代码。我使用IP地址跳过DNS检查,并将代理设置为空。(我在某个地方读到这样做可以防止它挂起。我想。这是关于网络代理自动检测的。)我可以验证下载链接(除非你需要,否则我不会在这里显示)是真实的,并通过在网络浏览器中打开它来下载文件。无论如何,这里是我的代码片段:
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"););