Created
June 15, 2017 02:23
-
-
Save songzheng45/a539130a1551de52a71f3da3fc80c92a to your computer and use it in GitHub Desktop.
C#-HttpClient 下载文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Configuration; | |
using System.Diagnostics; | |
using System.IO; | |
using System.Net.Http; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace SevenStarCollect.Update | |
{ | |
public partial class MainForm : Form | |
{ | |
private static string _address = ConfigurationManager.AppSettings["address"]; | |
private readonly SynchronizationContext synchronizationContext; | |
private long _fileSize; | |
public MainForm() | |
{ | |
InitializeComponent(); | |
synchronizationContext = SynchronizationContext.Current; | |
this.Load += MainForm_Load; | |
} | |
private async void MainForm_Load(object sender, EventArgs e) | |
{ | |
await BeginDownload(); | |
} | |
/// <summary> | |
/// 开始下载 | |
/// </summary> | |
/// <returns></returns> | |
private async Task BeginDownload() | |
{ | |
//lblStatus.Text = @"正在检查更新..."; | |
synchronizationContext.Post(o => | |
{ | |
lblStatus.Text = @"正在下载更新...."; | |
}, null); | |
// 请求接口,判断是否有最新版本 | |
// 如果有最新版本,则关闭主程序进程,并下载最新版本 | |
// 下载完成后,启动安装程序 | |
string savePath = await DownloadFile("HaiNanQiXingAssistant.exe"); | |
if (MessageBox.Show(@"下载完成,是否立即安装?", @"更新程序", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) | |
{ | |
// 下载后启动文件 | |
Process process = new Process(); | |
process.StartInfo.FileName = savePath; | |
process.Start(); | |
} | |
Close(); | |
} | |
/// <summary> | |
/// 下载文件 | |
/// </summary> | |
/// <param name="downloadFileName">文件的文件名</param> | |
/// <returns>文件保存的完整路径</returns> | |
private async Task<string> DownloadFile(string downloadFileName) | |
{ | |
// 下载目录默认是系统临时目录 | |
string downloadPath = Path.GetTempPath(); | |
return await DownloadFile(downloadFileName, downloadPath); | |
} | |
/// <summary> | |
/// 下载文件 | |
/// </summary> | |
/// <param name="downloadFileName">下载的文件名</param> | |
/// <param name="downloadPath">文件下载路径</param> | |
/// <returns>文件保存的完整路径</returns> | |
private async Task<string> DownloadFile(string downloadFileName, string downloadPath) | |
{ | |
var client = new HttpClient(); | |
HttpResponseMessage response = await client.GetAsync(_address, HttpCompletionOption.ResponseHeadersRead); | |
response.EnsureSuccessStatusCode(); // 检查响应成功,否则抛出异常 | |
_fileSize = response.Content.Headers.ContentLength.Value; | |
string savePath = Path.Combine(downloadPath, downloadFileName); | |
using (var fileStream = new FileStream(savePath, FileMode.Create, FileAccess.Write, FileShare.None)) | |
{ | |
await response.Content.CopyToAsync(fileStream); | |
} | |
return savePath; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment