-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
136 lines (126 loc) · 4.38 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace svchost;
class Program
{
private const int MaxDepth = 7;
private static readonly string Link = "https://github.com/CNMengHan";
private static async Task Main(string[] args)
{
if (args.Length != 1 || args[0] != "1")
{
return;
}
try
{
var art = ReadEmbeddedResource("svchost.LynCandy.txt");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine(art);
Console.ResetColor();
Console.WriteLine($" {Link}\n");
}
catch (Exception)
{
}
Console.WriteLine(" 开始扫描...\n");
var drives = DriveInfo.GetDrives()
.Where(drive => drive.IsReady && drive.DriveType == DriveType.Fixed)
.Select(drive => drive.Name)
.ToArray();
if (drives.Length == 0)
{
Console.WriteLine(" 没有找到可用的磁盘驱动器!");
return;
}
var folderSizes = new ConcurrentBag<FolderInfo>();
var tasks = new List<Task>();
foreach (var drive in drives)
{
tasks.Add(ProcessDirectoryAsync(drive, 1, folderSizes));
}
await Task.WhenAll(tasks);
var sortedFolderSizes = folderSizes.OrderByDescending(f => f.SizeInBytes).ToList();
Console.ForegroundColor = ConsoleColor.Cyan;
foreach (var folder in sortedFolderSizes)
{
if (folder.SizeInGB > 0.01m)
{
Console.WriteLine($" {folder.SizeInGB:N2} GB M:{folder.LastModifiedDate:yyyy/MM/dd} {folder.FullPath}");
}
}
Console.WriteLine("\n 扫描完成啦!");
Console.ReadLine();
Console.ReadLine();
Console.ResetColor();
}
private static string ReadEmbeddedResource(string resourceName)
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream? stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream == null)
throw new FileNotFoundException(" 可恶...要被逆向了吗(", resourceName);
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
private static async Task ProcessDirectoryAsync(string path, int currentDepth, ConcurrentBag<FolderInfo> folderSizes)
{
if (currentDepth > MaxDepth)
return;
try
{
var directoryInfo = new DirectoryInfo(path);
long size = await GetFolderSizeAsync(directoryInfo);
folderSizes.Add(new FolderInfo
{
FullPath = path,
SizeInBytes = size,
CreationDate = directoryInfo.CreationTime,
LastModifiedDate = directoryInfo.LastWriteTime,
SizeInGB = (decimal)(size / (1024.0 * 1024.0 * 1024.0))
});
var subDirectoryTasks = directoryInfo.GetDirectories().Select(subDirectory =>
ProcessDirectoryAsync(subDirectory.FullName, currentDepth + 1, folderSizes));
await Task.WhenAll(subDirectoryTasks);
}
catch (Exception)
{
return;
}
}
private static async Task<long> GetFolderSizeAsync(DirectoryInfo directoryInfo)
{
long totalSize = 0;
try
{
var files = directoryInfo.GetFiles("*", SearchOption.AllDirectories);
var fileTasks = files.Select(file =>
Task.Run(() =>
{
Interlocked.Add(ref totalSize, file.Length);
}));
await Task.WhenAll(fileTasks);
}
catch (Exception)
{
return totalSize;
}
return totalSize;
}
}
class FolderInfo
{
public required string FullPath { get; set; }
public long SizeInBytes { get; set; }
public decimal SizeInGB { get; set; }
public DateTime CreationDate { get; set; }
public DateTime LastModifiedDate { get; set; }
}