using Microsoft.VisualBasic.FileIO;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace j.Dev
{
public class DevHelpers
{
public static async Task Download()
{
var version = "1.0.2";
var name = "Framework45-" + DateTimeToFileString(DateTime.Now);
var fileName = $"{name}.zip";
var url = $"https://www.nuget.org/api/v2/package/Microsoft.NETFramework.ReferenceAssemblies.net45/{version}";
await DownloadFile(fileName, url);
ZipFile.ExtractToDirectory(fileName, name);
var from = Path.Join(name, @"build\.NETFramework\v4.5\");
var to = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5";
FileSystem.CopyDirectory(from, to, UIOption.AllDialogs);
}
private static string DateTimeToFileString(DateTime d)
{
return d.ToString("yyyy-dd-M--HH-mm-ss");
}
private static async Task DownloadFile(string fileName, string url)
{
var uri = new Uri(url);
HttpClient client = new HttpClient();
var response = await client.GetAsync(uri);
using (var fs = new FileStream(
fileName,
FileMode.CreateNew))
{
await response.Content.CopyToAsync(fs);
}
}
}
}
在我的例子中,我需要下载4.0和4.5,下面是同时下载4.0和4.5的代码:
using Microsoft.VisualBasic.FileIO;
using System.IO.Compression;
namespace j.Dev
{
/// <summary>
/// Example Usage: await DevHelpers.DownloadAndCopyFramework4_0And4_5();
/// </summary>
public class DevHelpers
{
public static async Task DownloadAndCopyFramework4_0And4_5()
{
await DownloadAndCopyFramework4_0();
await DownloadAndCopyFramework4_5();
}
public static async Task DownloadAndCopyFramework4_5()
{
await DownloadAndCopyFrameworkGeneric("net45", "v4.5", "1.0.2");
}
public static async Task DownloadAndCopyFramework4_0()
{
await DownloadAndCopyFrameworkGeneric("net40", "v4.0", "1.0.2");
}
public static async Task DownloadAndCopyFrameworkGeneric(string netVersion, string folder, string nugetVersion)
{
var name = netVersion + "-" + DateTimeToFileString(DateTime.Now);
var fileName = $"{name}.zip";
var url = $"https://www.nuget.org/api/v2/package/Microsoft.NETFramework.ReferenceAssemblies.{netVersion}/{nugetVersion}";
await DownloadFile(fileName, url);
ZipFile.ExtractToDirectory(fileName, name);
var from = Path.Join(name, @"build\.NETFramework\" + folder);
var to = @"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\" + folder;
FileSystem.CopyDirectory(from, to, UIOption.AllDialogs);
}
private static string DateTimeToFileString(DateTime d)
{
return d.ToString("yyyy-dd-M--HH-mm-ss");
}
private static async Task DownloadFile(string fileName, string url)
{
var uri = new Uri(url);
HttpClient client = new HttpClient();
var response = await client.GetAsync(uri);
using (var fs = new FileStream(
fileName,
FileMode.CreateNew))
{
await response.Content.CopyToAsync(fs);
}
}
}
}