好的,所以我是编程新手,我想制作一个带有进度条的工具条,显示从 Internet 获取并打开的文件的进度,这是代码片段http://pastebin.com/3EKrFb4K和它在代码中没有给出错误,但是当我调试它时,当我尝试下载并打开文件时(一旦它甚至不这样做)http://gyazo.com/44634914669d81b1c20d3d26f2dd3ad8我想知道它是否只是简单的东西我写的代码行不通?它是一个工具条进度条,所以我必须做一些特别的事情还是什么?请提前帮助和感谢。
问问题
328 次
1 回答
0
问题是您在下载文件之前打开文件:
WebClient web = new WebClient(); web.DownloadFileAsync(new Uri(direct_exe_from_url), 文件路径); // 这个方法是异步的 web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);
Process.Start(文件路径); // 你在文件还在下载的时候打开它。
您需要在 downloadFileCompleted 处理程序中执行进程启动。
这应该有效:
private void startBotToolStripMenuItem_Click(object sender, EventArgs e)
{
string directoryPath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\";
string filepath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\" + "Minecraft.exe";
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
string direct_exe_from_url = "http://rs542p2.rapidshare.com/cgi-bin/rsapi.cgi?sub=download&fileid=1764003915&filename=Minecraft.exe&cookie=F2CB284BDC9920808D8494CA4EB46F0935AB22D79EC69D6D130C21FB6AD2A0A1EB413347302A46C5FB1A39599DF740D6&directstart=1";
WebClient web = new WebClient();
web.DownloadFileAsync(new Uri(direct_exe_from_url), filepath);
web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);
web.DownloadFileCompleted += new AsyncCompletedEventHandler (DownloadFileCallback);
}
void web_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
ProgressBar1.Value = e.ProgressPercentage;
}
void DownloadFileCallback(object sender, AsyncCompletedEventArgs e)
{
string filepath = Environment.GetEnvironmentVariable("AppData") + "\\Downloaded Files\\" + "Minecraft.exe";
Process.Start(filepath);
}
于 2013-04-18T23:11:46.520 回答