2015-09-05 29 views
1

我在我的应用程序中允许用户下载最新版本的应用程序的代码。应用程序下载完成后,如果用户想要打开文件位置以查看该文件,则会打开提示。已完成异步下载,而不是一次

但是,该工具启动两个消息框而不是一次。我不确定我是否错过了一些东西。

private void BTN_GNV_MouseUp(object sender, MouseButtonEventArgs e) 
{ 
    string URLDir = "http://shard.combatkingz.com/downloads/"; 
    string URLName = "DayZ Config Tweak tool v" + Properties.Settings.Default.AvailableVersion + ".exe"; 
    string URLFull = ""; 
    using (WebClient DLWC = new WebClient()) 
    { 
     URLFull = URLDir + URLName; 
     GlobalVars.DLPath = System.Environment.CurrentDirectory + "\\" + URLName; 
     try 
     { 
      DLWC.DownloadFileAsync(new Uri(URLFull), GlobalVars.DLPath); 
      DLWC.DownloadProgressChanged += DLWC_DownloadProgressChanged; 
     } 
     catch 
     { 
      MessageBox.Show("There was an error downloading the file.", GlobalVars.APPNAME, MessageBoxButton.OK, MessageBoxImage.Error); 
#if DEBUG 
#else 
      AddDownloadToDB("Failed"); 
#endif 
     } 
    } 
} 
void DLWC_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
{ 
    PB_GNV.Width = (BTN_GNV.Width/100) * e.ProgressPercentage; 
    if (PB_GNV.Width == BTN_GNV.Width && e.TotalBytesToReceive == e.BytesReceived) 
    { 
     MessageBoxResult nav = MessageBox.Show("New version downloaded. Do you want to navigate to the folder?", GlobalVars.APPNAME, MessageBoxButton.YesNo, MessageBoxImage.Error); 
     if (nav == MessageBoxResult.Yes) 
     { 
      string argument = @"/select, " + @GlobalVars.DLPath; 
      System.Diagnostics.Process.Start("explorer.exe", argument); 
#if DEBUG 
#else 
      AddDownloadToDB("Success"); 
#endif 
     } 
    } 
} 

回答

1

我怀疑DownloadProgressChanged事件在接收到最后一个字节并在文件完成时触发。使用DownloadFileCompleted事件应该可以解决问题。

+0

是的。 DownloadFileCompleted处理程序修复了这个问题。它不再显示两次并且完美地工作。谢谢! – DethoRhyne

相关问题