XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

XFEExtension

【DLL】XFE各类拓展是一个C#的DLL库,旨在优化C#代码中常用语句的使用,并提供更简洁的访问方式,同时提供Xunit测试框架,快速搭建服务器/客户端,免费ChatGPTAPI接口,免费通讯服务器,XFE下载器,新增格式等

公开
关注 0 Fork 0 Star 0
返回提交历史

XFEstudio/XFEExtension

文件下载器完成

5cc72a1
XFE工作室室长 <mail@xfegzs.com>
提交于

代码差异

4 个文件 +119 -53
Modified XFE各类拓展.NetCore/FileExtension/FileExtension.cs +35 -1
@@ -1,4 +1,5 @@
1 using System.Runtime.Serialization.Formatters.Binary;
1 using System.Diagnostics;
2 using System.Runtime.Serialization.Formatters.Binary;
2 3 using System.Text;
3 4
4 5 namespace XFE各类拓展.NetCore.FileExtension;
@@ -192,5 +193,38 @@ public static class FileExtension
192 193 }
193 194 return string.Format("{0:0.##} {1}", len, sizes[order]);
194 195 }
196 /// <summary>
197 /// 获取占用某个文件的程序
198 /// </summary>
199 /// <param name="filePath">文件路径</param>
200 /// <returns></returns>
201 public static Process? GetOwningProcess(this string filePath)
202 {
203 try
204 {
205 // 获取所有正在运行的进程
206 Process[] processes = Process.GetProcesses();
195 207
208 foreach (Process process in processes)
209 {
210 try
211 {
212 // 获取进程打开的文件句柄信息
213 foreach (ProcessModule module in process.Modules)
214 {
215 if (module.FileName.Equals(filePath, StringComparison.OrdinalIgnoreCase))
216 {
217 return process; // 返回占用文件的进程
218 }
219 }
220 }
221 catch (Exception)
222 {
223 // 忽略无法访问的进程
224 }
225 }
226 }
227 catch { }
228 return null; // 如果没有找到占用文件的进程,返回null
229 }
196 230 }
Added XFE各类拓展.NetCore/WebExtension/FileDownloadedEventArgs.cs +25 -0
@@ -0,0 +1,25 @@
1 namespace XFE各类拓展.NetCore.WebExtension;
2
3 /// <summary>
4 /// 字符下载事件
5 /// </summary>
6 public class FileDownloadedEventArgs(byte[] currentBuffer, long downloadedBufferSize, long? totalBufferSize, bool downloaded) : EventArgs
7 {
8
9 /// <summary>
10 /// 当前下载到的字节
11 /// </summary>
12 public byte[] CurrentBuffer { get; } = currentBuffer;
13 /// <summary>
14 /// 总计下载的字节长度
15 /// </summary>
16 public long DownloadedBufferSize { get; } = downloadedBufferSize;
17 /// <summary>
18 /// 总共需要下载的字节大小
19 /// </summary>
20 public long? TotalBufferSize { get; } = totalBufferSize;
21 /// <summary>
22 /// 是否下载完成
23 /// </summary>
24 public bool Downloaded { get; } = downloaded;
25 }
Modified XFE各类拓展.NetCore/WebExtension/WebExtension.cs +1 -52
@@ -1,5 +1,4 @@
1 1 using System.Text;
2 using XFE各类拓展;
3 2 using XFE各类拓展.NetCore.TaskExtension;
4 3
5 4 namespace XFE各类拓展.NetCore.WebExtension;
@@ -153,54 +152,4 @@ public static class WebExtension
153 152 return null;
154 153 }
155 154 }
156 }
157 public class XFEDownloader
158 {
159 public event EventHandler<FileDownloadedEventArgs>? BufferDownloaded;
160 public string? DownloadUrl { get; set; }
161 public string? SavePath { get; set; }
162 public async Task Download()
163 {
164 using (HttpClient client = new HttpClient())
165 {
166 using (HttpResponseMessage response = await client.GetAsync(DownloadUrl, HttpCompletionOption.ResponseHeadersRead))
167 {
168 response.EnsureSuccessStatusCode();
169
170 long? totalFileSize = response.Content.Headers.ContentLength;
171
172 using (Stream contentStream = await response.Content.ReadAsStreamAsync())
173 {
174 using (FileStream fileStream = new FileStream(SavePath!, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true))
175 {
176 byte[] buffer = new byte[8192];
177 long totalRead = 0;
178 int read;
179
180 while ((read = await contentStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
181 {
182 await fileStream.WriteAsync(buffer, 0, read);
183 totalRead += read;
184
185 if (totalFileSize.HasValue)
186 {
187 double percentage = (double)totalRead / totalFileSize.Value * 100;
188 Console.WriteLine($"Downloaded {totalRead}/{totalFileSize} bytes ({percentage:F2}%)");
189 }
190 else
191 {
192 Console.WriteLine($"Downloaded {totalRead} bytes");
193 }
194 }
195 }
196 }
197 }
198 }
199 }
200 }
201 public class FileDownloadedEventArgs : EventArgs
202 {
203 public long CurrentBufferSize { get; set; }
204 public long DownloadedBufferSize { get; set; }
205 public long TotalBufferSize { get; set; }
206 }
155 }
Added XFE各类拓展.NetCore/WebExtension/XFEDownloader.cs +58 -0
@@ -0,0 +1,58 @@
1 using XFE各类拓展.NetCore.DelegateExtension;
2
3 namespace XFE各类拓展.NetCore.WebExtension;
4
5 /// <summary>
6 /// XFE下载器
7 /// </summary>
8 public class XFEDownloader
9 {
10 /// <summary>
11 /// 字节下载事件
12 /// </summary>
13 public event XFEEventHandler<XFEDownloader, FileDownloadedEventArgs>? BufferDownloaded;
14 /// <summary>
15 /// 目标下载地址
16 /// </summary>
17 public required string DownloadUrl { get; set; }
18 /// <summary>
19 /// 储存位置
20 /// </summary>
21 public required string SavePath { get; set; }
22 /// <summary>
23 /// 已下载
24 /// </summary>
25 public bool Downloaded { get; }
26 /// <summary>
27 /// 开始下载
28 /// </summary>
29 /// <param name="continueFromLastDownload"></param>
30 /// <returns></returns>
31 public async Task Download(bool continueFromLastDownload = true)
32 {
33 using var client = new HttpClient();
34 var continueDownload = continueFromLastDownload && File.Exists(SavePath);
35 using var fileStream = new FileStream(SavePath, continueDownload ? FileMode.Append : FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);
36 long totalRead = 0;
37 long lastBufferDownloadSize = fileStream.Length;
38 if (continueDownload)
39 {
40 client.DefaultRequestHeaders.Range = new System.Net.Http.Headers.RangeHeaderValue(lastBufferDownloadSize, null);
41 totalRead = lastBufferDownloadSize;
42 }
43 using var response = await client.GetAsync(DownloadUrl, HttpCompletionOption.ResponseHeadersRead);
44 response.EnsureSuccessStatusCode();
45 long? totalFileSize = response.Content.Headers.ContentLength + lastBufferDownloadSize;
46 using var contentStream = await response.Content.ReadAsStreamAsync();
47 byte[] buffer = new byte[8192];
48 int currentRead;
49 while ((currentRead = await contentStream.ReadAsync(buffer)) > 0)
50 {
51 await fileStream.WriteAsync(buffer.AsMemory(0, currentRead));
52 totalRead += currentRead;
53 var bufferCopy = new byte[8192];
54 bufferCopy.CopyTo(buffer, 0);
55 BufferDownloaded?.Invoke(this, new FileDownloadedEventArgs(bufferCopy, totalRead, totalFileSize, currentRead != 8192));
56 }
57 }
58 }