1- using System . Net ;
2-
3- #pragma warning disable SYSLIB0014 // 类型或成员已过时
4-
5- namespace Common . Algorithm . Interop ;
1+ namespace Common . Algorithm . Interop ;
62
73public static class Environment
84{
@@ -28,7 +24,7 @@ public static class Environment
2824 private static readonly List < string > CoreFiles = new ( )
2925 {
3026 "Math.dll" ,
31- "Hash.dll"
27+ "Hash.dll" ,
3228 } ;
3329
3430 /// <summary>
@@ -47,7 +43,7 @@ public static class Environment
4743 /// 检查环境是否就绪, 核心文件是否存在
4844 /// </summary>
4945 /// <returns>环境是否就绪</returns>
50- public static bool CheckEnvironment ( )
46+ public static bool Check ( )
5147 {
5248 foreach ( string fn in CoreFiles )
5349 {
@@ -61,52 +57,35 @@ public static bool CheckEnvironment()
6157 /// 安装环境
6258 /// <paramref name="im">安装方式</paramref>
6359 /// </summary>
64- public static void InstallEnvironment ( InstallMethod im = InstallMethod . WebClient )
60+ public static async Task InstallAsync ( InstallMethod im = InstallMethod . HttpClient )
6561 {
6662 if ( ! Directory . Exists ( DllPath ) )
6763 Directory . CreateDirectory ( DllPath ) ;
68- switch ( im )
69- {
70- case InstallMethod . WebClient :
71- WebClient wc = new ( ) ;
72- foreach ( string fn in CoreFiles )
73- {
74- if ( ! File . Exists ( $ "{ DllPath } { fn } ") )
75- {
76- wc . DownloadFile (
77- new Uri ( $ "{ _cloudUrl } { _version } /{ fn } ", UriKind . Absolute ) ,
78- Path . GetFullPath ( $ "{ DllPath } { fn } ") ) ;
79- }
80- }
81- wc . Dispose ( ) ;
82- break ;
83- }
84- }
8564
86- /// <summary>
87- /// 异步安装环境
88- /// <paramref name="im">异步安装方式</paramref>
89- /// </summary>
90- public static async Task InstallEnvironmentAsync ( InstallMethodAsync im = InstallMethodAsync . WebClientAsync )
91- {
92- if ( ! Directory . Exists ( DllPath ) )
93- Directory . CreateDirectory ( DllPath ) ;
9465 switch ( im )
9566 {
96- case InstallMethodAsync . WebClientAsync :
97- var wc = new WebClient ( ) ;
98- foreach ( string fn in CoreFiles )
67+ case InstallMethod . HttpClient :
68+
9969 {
100- if ( ! File . Exists ( $ "{ DllPath } { fn } ") )
70+ using var client = new HttpClient ( ) ;
71+
72+ foreach ( var fn in CoreFiles )
10173 {
102- await wc . DownloadFileTaskAsync (
103- new Uri ( $ "{ _cloudUrl } { _version } /{ fn } ", UriKind . Absolute ) ,
104- Path . GetFullPath ( $ "{ DllPath } { fn } ") ) ;
74+ var downloadUrl = $ "{ _cloudUrl } { _version } /{ fn } ";
75+ var savePath = Path . GetFullPath ( $ "{ DllPath } { fn } ") ;
76+
77+ if ( ! File . Exists ( savePath ) )
78+ {
79+ using var stream = await client . GetStreamAsync ( downloadUrl ) ;
80+
81+ using var fileStream = File . Create ( savePath ) ;
82+
83+ await stream . CopyToAsync ( fileStream ) ;
84+
85+ await fileStream . FlushAsync ( ) ;
86+ }
10587 }
10688 }
107- wc ? . Dispose ( ) ;
108- break ;
109- case InstallMethodAsync . Http :
11089
11190 break ;
11291 }
@@ -117,16 +96,6 @@ await wc.DownloadFileTaskAsync(
11796 /// </summary>
11897 public enum InstallMethod
11998 {
120- WebClient
121- }
122-
123- /// <summary>
124- /// 安装环境下载方式
125- /// </summary>
126- public enum InstallMethodAsync
127- {
128- WebClientAsync , Http
99+ HttpClient = 1 ,
129100 }
130101}
131-
132- #pragma warning restore SYSLIB0014 // 类型或成员已过时
0 commit comments