Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
692 lines (641 loc) · 28.2 KB
/
Copy pathProgram.cs
File metadata and controls
692 lines (641 loc) · 28.2 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
// BitChecker - 拖入文件判断 32/64 位
// 用法: 拖入文件到窗口; 或命令行: BitChecker.exe <文件...>
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32.SafeHandles;
namespace BitChecker
{
static class Detector
{
public static string Describe(string path)
{
try
{
if (Directory.Exists(path)) return "这是文件夹,请拖入文件而不是文件夹";
if (!File.Exists(path)) return "文件不存在: " + path;
if (path.ToLowerInvariant().EndsWith(".lnk"))
{
string t = ResolveLnk(path);
if (t != null && t.Length > 0)
{
if (!File.Exists(t)) return "快捷方式指向: " + t + " (目标不存在)";
path = t;
}
}
byte[] head = ReadHead(path, 8);
if (head == null || head.Length < 2) return "无法读取文件(可能被占用或无权限)";
if (head[0] == 0x4D && head[1] == 0x5A)
{
string r = CheckPe(path);
if (r != null) return r;
return "MZ 头但缺少 PE 签名(可能是 DOS 程序、损坏或非 Windows 程序)";
}
if (head.Length >= 8 && head[0] == 0xD0 && head[1] == 0xCF && head[2] == 0x11 && head[3] == 0xE0 &&
head[4] == 0xA1 && head[5] == 0xB1 && head[6] == 0x1A && head[7] == 0xE1)
{
string lower = path.ToLowerInvariant();
if (lower.EndsWith(".msu")) return "MSU 更新包(内含 CAB/程序,请解包后再判断位宽)";
return CheckMsi(path);
}
if (head.Length >= 4 && head[0] == 0x50 && head[1] == 0x4B)
return CheckZip(path);
if (head.Length >= 4 && head[0] == 0x4D && head[1] == 0x53 && head[2] == 0x43 && head[3] == 0x46)
return "CAB 安装包:请解压后判断内部 exe 的位宽";
return "无法识别(" + Path.GetExtension(path) + "):支持 exe/dll/sys/msi/msix/appx/cab 等 Windows 安装包,直接拖入即可";
}
catch (Exception ex)
{
return "错误: " + ex.Message;
}
}
static byte[] ReadHead(string path, int size)
{
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[size];
int n = fs.Read(b, 0, size);
if (n < size) Array.Resize(ref b, n);
return b;
}
}
static byte[] ReadFileCapped(string path)
{
FileInfo fi = new FileInfo(path);
const int cap = 64 * 1024 * 1024;
if (fi.Length <= cap) return File.ReadAllBytes(path);
using (FileStream fs = File.OpenRead(path))
{
byte[] b = new byte[cap];
int n = fs.Read(b, 0, b.Length);
if (n < b.Length) Array.Resize(ref b, n);
return b;
}
}
static string CheckPe(string path)
{
byte[] b = ReadHead(path, 65536);
if (b == null || b.Length < 64) return null;
return PeMachine(b, b.Length);
}
static string PeMachine(byte[] b, int n)
{
if (n < 64 || b[0] != 0x4D || b[1] != 0x5A) return null;
int peOff = BitConverter.ToInt32(b, 0x3C);
if (peOff < 0 || peOff + 24 > n) return null;
if (b[peOff] != 0x50 || b[peOff + 1] != 0x45 || b[peOff + 2] != 0 || b[peOff + 3] != 0) return null;
ushort m = BitConverter.ToUInt16(b, peOff + 4);
switch (m)
{
case 0x8664: return "64 位 (x64)";
case 0xAA64: return "64 位 (ARM64)";
case 0x0200: return "64 位 (IA64)";
case 0x01F1: return "64 位 (PowerPC)";
case 0x5064: return "64 位 (RISC-V)";
case 0x5128: return "128 位 (RISC-V)";
case 0x014C:
{
string clr = ClrArch(b, n, peOff);
if (clr != null) return clr;
return "32 位 (x86)";
}
case 0x01C4: return "32 位 (ARMv7)";
case 0x01C0: return "32 位 (ARM)";
case 0x01C2: return "32 位 (ARM Thumb-2)";
case 0x0166: return "32 位 (MIPS)";
case 0x0184: return "32 位 (MIPS-16)";
case 0x01F0: return "32 位 (PowerPC)";
case 0x5032: return "32 位 (RISC-V)";
default: return "PE 未知架构 (0x" + m.ToString("X4") + ")";
}
}
static string ClrArch(byte[] b, int n, int peOff)
{
try
{
int opt = peOff + 24;
if (opt + 4 > n) return null;
ushort magic = BitConverter.ToUInt16(b, opt);
ushort numSections = BitConverter.ToUInt16(b, peOff + 6);
ushort optSize = BitConverter.ToUInt16(b, peOff + 20);
int ddBase = magic == 0x20B ? opt + 112 : opt + 96;
int clrDir = ddBase + 14 * 8;
if (clrDir + 8 > n) return null;
uint clrRva = BitConverter.ToUInt32(b, clrDir);
uint clrSize = BitConverter.ToUInt32(b, clrDir + 4);
if (clrRva == 0 || clrSize < 20) return null;
int secTab = opt + optSize;
if (secTab + numSections * 40 > n) return null;
for (int i = 0; i < numSections; i++)
{
int s = secTab + i * 40;
uint vaddr = BitConverter.ToUInt32(b, s + 12);
uint vsize = BitConverter.ToUInt32(b, s + 8);
uint rsize = BitConverter.ToUInt32(b, s + 16);
uint raddr = BitConverter.ToUInt32(b, s + 20);
if (clrRva >= vaddr && clrRva < vaddr + (vsize > 0 ? vsize : rsize))
{
long fo = raddr + (clrRva - vaddr);
if (fo + 20 > n) return null;
uint flags = BitConverter.ToUInt32(b, (int)fo + 16);
if ((flags & 1) != 0)
{
if ((flags & 2) != 0) return "32 位 (.NET, 仅限 32 位)";
return "通用 (.NET 任意 CPU, 随系统自动运行 32/64 位)";
}
return null;
}
}
return null;
}
catch { return null; }
}
static string CheckMsi(string path)
{
byte[] data = ReadFileCapped(path);
if (data == null) return "无法读取 MSI";
string tpl = GetMsiTemplate(data);
if (tpl != null)
{
tpl = tpl.Trim().Trim('\0');
if (tpl.StartsWith("x64") || tpl.StartsWith("AMD64")) return "64 位 MSI (Template: " + tpl + ")";
if (tpl.StartsWith("Intel64")) return "64 位 MSI - IA64 (Template: " + tpl + ")";
if (tpl.StartsWith("Arm64")) return "64 位 MSI - ARM64 (Template: " + tpl + ")";
if (tpl.StartsWith("Intel")) return "32 位 MSI (Template: " + tpl + ")";
return "MSI 位宽未知 (Template: " + tpl + ")";
}
string pe = ScanEmbeddedPe(data);
if (pe != null) return "MSI 内嵌程序: " + pe;
if (ContainsAscii(data, "x64;") || ContainsAscii(data, "AMD64;")) return "64 位 MSI";
if (ContainsAscii(data, "Intel;")) return "32 位 MSI";
return "MSI:未找到位宽信息";
}
static string GetMsiTemplate(byte[] d)
{
try
{
if (d.Length < 512) return null;
int sectorShift = BitConverter.ToUInt16(d, 0x1E);
int miniShift = BitConverter.ToUInt16(d, 0x20);
if (sectorShift < 7 || sectorShift > 16) return null;
int sectorSize = 1 << sectorShift;
int miniSize = 1 << miniShift;
uint firstDir = BitConverter.ToUInt32(d, 0x30);
uint cutoff = BitConverter.ToUInt32(d, 0x38);
uint firstMiniFat = BitConverter.ToUInt32(d, 0x3C);
uint numMiniFat = BitConverter.ToUInt32(d, 0x40);
var fatSectors = new List<uint>();
for (int i = 0; i < 109; i++)
{
uint v = BitConverter.ToUInt32(d, 0x4C + i * 4);
if (v < 0xFFFFFFF8) fatSectors.Add(v);
}
uint difat = BitConverter.ToUInt32(d, 0x44);
uint numDifat = BitConverter.ToUInt32(d, 0x48);
int guard = 0;
while (difat < 0xFFFFFFF8 && numDifat > 0 && guard++ < 100)
{
long off = (difat + 1L) * sectorSize;
if (off + sectorSize > d.Length) break;
for (int i = 0; i < sectorSize / 4 - 1; i++)
{
uint v = BitConverter.ToUInt32(d, (int)off + i * 4);
if (v < 0xFFFFFFF8) fatSectors.Add(v);
}
difat = BitConverter.ToUInt32(d, (int)(off + sectorSize - 4));
numDifat--;
}
var fat = new List<uint>();
foreach (uint s in fatSectors)
{
long off = (s + 1L) * sectorSize;
if (off + sectorSize > d.Length) break;
for (int i = 0; i < sectorSize / 4; i++)
fat.Add(BitConverter.ToUInt32(d, (int)(off + i * 4)));
}
var dirSectors = new List<uint>();
uint cur = firstDir;
guard = 0;
while (cur < 0xFFFFFFF8 && (cur + 1L) * sectorSize + 128 <= d.Length && guard++ < 100000)
{
dirSectors.Add(cur);
if (cur >= fat.Count) break;
cur = fat[(int)cur];
}
if (dirSectors.Count == 0) return null;
Func<uint, int, long> DirPos = delegate(uint sector, int idx)
{
return (sector + 1L) * sectorSize + idx * 128L;
};
Func<long, string> DirName = delegate(long pos)
{
int len = BitConverter.ToUInt16(d, (int)pos + 0x40);
if (len < 2) return "";
len = Math.Min(len - 2, 64);
return Encoding.Unicode.GetString(d, (int)pos, len);
};
Func<long, int, uint> DirU32 = delegate(long pos, int off)
{
return BitConverter.ToUInt32(d, (int)pos + off);
};
long rootPos = DirPos(dirSectors[0], 0);
uint rootStart = DirU32(rootPos, 0x74);
long rootSize = BitConverter.ToInt64(d, (int)rootPos + 0x78);
uint rootChild = DirU32(rootPos, 0x4C);
Func<uint, uint> entryLeft = delegate(uint nd)
{
long p = DirPos(dirSectors[(int)(nd / 4)], (int)(nd % 4));
return DirU32(p, 0x44);
};
Func<uint, uint> entryRight = delegate(uint nd)
{
long p = DirPos(dirSectors[(int)(nd / 4)], (int)(nd % 4));
return DirU32(p, 0x48);
};
Func<uint, string> entryName = delegate(uint nd)
{
long p = DirPos(dirSectors[(int)(nd / 4)], (int)(nd % 4));
return DirName(p);
};
Func<uint, long> entryStart = delegate(uint nd)
{
long p = DirPos(dirSectors[(int)(nd / 4)], (int)(nd % 4));
return DirU32(p, 0x74);
};
Func<uint, long> entrySize = delegate(uint nd)
{
long p = DirPos(dirSectors[(int)(nd / 4)], (int)(nd % 4));
return BitConverter.ToInt64(d, (int)p + 0x78);
};
uint node = rootChild;
uint found = 0xFFFFFFFF;
guard = 0;
while (node < 0xFFFFFFF8 && guard++ < 100000)
{
if (node / 4 >= dirSectors.Count) break;
string nm = entryName(node);
int cmp = string.Compare(nm, "SummaryInformation", StringComparison.OrdinalIgnoreCase);
if (cmp == 0) { found = node; break; }
node = cmp < 0 ? entryLeft(node) : entryRight(node);
}
if (found == 0xFFFFFFFF) return null;
long stStart = entryStart(found);
long stSize = entrySize(found);
if (stSize <= 0) return null;
byte[] stream;
if (stSize < cutoff)
{
byte[] mini = ReadChain(d, fat, sectorSize, rootStart, rootSize);
if (mini == null) return null;
var miniFat = new List<uint>();
uint mc = firstMiniFat;
guard = 0;
while (mc < 0xFFFFFFF8 && guard++ < 10000)
{
long off = (mc + 1L) * sectorSize;
if (off + sectorSize > d.Length) break;
for (int i = 0; i < sectorSize / 4; i++)
miniFat.Add(BitConverter.ToUInt32(d, (int)(off + i * 4)));
if (mc >= fat.Count) break;
mc = fat[(int)mc];
}
stream = ReadMiniChain(mini, miniFat, (uint)stStart, stSize, miniSize);
}
else
{
stream = ReadChain(d, fat, sectorSize, (uint)stStart, stSize);
}
if (stream == null || stream.Length < 0x30) return null;
return ParseTemplate(stream);
}
catch { return null; }
}
static byte[] ReadChain(byte[] d, List<uint> fat, int sectorSize, uint start, long size)
{
try
{
if (size <= 0 || size > 1L * 1024 * 1024 * 1024) return null;
byte[] outB = new byte[size];
long written = 0;
uint cur = start;
int guard = 0;
while (written < size && guard++ < 1000000)
{
long pos = (cur + 1L) * sectorSize;
int take = (int)Math.Min(sectorSize, size - written);
if (pos < 0 || pos + take > d.Length) break;
Array.Copy(d, pos, outB, written, take);
written += take;
if (cur >= fat.Count) break;
cur = fat[(int)cur];
if (cur >= 0xFFFFFFF8) break;
}
return outB;
}
catch { return null; }
}
static byte[] ReadMiniChain(byte[] mini, List<uint> miniFat, uint start, long size, int miniSize)
{
try
{
if (size <= 0) return null;
byte[] outB = new byte[size];
long written = 0;
uint cur = start;
int guard = 0;
while (written < size && guard++ < 1000000)
{
long pos = cur * (long)miniSize;
int take = (int)Math.Min(miniSize, size - written);
if (pos < 0 || pos + take > mini.Length) break;
Array.Copy(mini, pos, outB, written, take);
written += take;
if (cur >= miniFat.Count) break;
cur = miniFat[(int)cur];
if (cur >= 0xFFFFFFF8) break;
}
return outB;
}
catch { return null; }
}
static string ParseTemplate(byte[] s)
{
if (s.Length < 0x30) return null;
uint secOff = BitConverter.ToUInt32(s, 0x2C);
if (secOff + 8 > s.Length) return null;
uint numProps = BitConverter.ToUInt32(s, (int)secOff + 4);
int align = (BitConverter.ToUInt16(s, 0x02) == 1) ? 8 : 4;
for (uint i = 0; i < numProps; i++)
{
long e = secOff + 8 + i * 8L;
if (e + 8 > s.Length) break;
uint id = BitConverter.ToUInt32(s, (int)e);
if (id != 7) continue;
uint off = BitConverter.ToUInt32(s, (int)e + 4);
if (off + 8 > s.Length) break;
uint type = BitConverter.ToUInt32(s, (int)off);
long v = off + 4;
long av = (v + align - 1) & ~(long)(align - 1);
if (type == 0x1E) return ReadCStringAscii(s, av);
if (type == 0x1F) return ReadCStringUnicode(s, av);
if (type == 0x41)
{
if (av + 4 > s.Length) break;
long len = BitConverter.ToUInt32(s, (int)av);
long start = (av + 4 + align - 1) & ~(long)(align - 1);
if (start + len > s.Length) break;
return Encoding.ASCII.GetString(s, (int)start, (int)len).Trim('\0');
}
}
return null;
}
static string ReadCStringAscii(byte[] s, long start)
{
long end = start;
while (end < s.Length && s[end] != 0 && end - start < 512) end++;
if (end - start < 1) return null;
return Encoding.ASCII.GetString(s, (int)start, (int)(end - start));
}
static string ReadCStringUnicode(byte[] s, long start)
{
long end = start;
while (end + 1 < s.Length && (s[end] != 0 || s[end + 1] != 0) && end - start < 1024) end += 2;
if (end - start < 2) return null;
return Encoding.Unicode.GetString(s, (int)start, (int)(end - start));
}
static string ScanEmbeddedPe(byte[] d)
{
int limit = Math.Min(d.Length - 64, 200 * 1024 * 1024);
if (limit <= 0) return null;
for (int i = 0; i < limit; i++)
{
if (d[i] != 0x4D || d[i + 1] != 0x5A) continue;
int peOff;
try { peOff = BitConverter.ToInt32(d, i + 0x3C); }
catch { continue; }
if (peOff < 0 || i + peOff + 24 > d.Length) continue;
if (d[i + peOff] == 0x50 && d[i + peOff + 1] == 0x45)
{
byte[] b = new byte[4096];
int n = Math.Min(4096, d.Length - i);
Array.Copy(d, i, b, 0, n);
string r = PeMachine(b, n);
if (r != null) return r;
}
}
return null;
}
static bool ContainsAscii(byte[] d, string s)
{
byte[] p = Encoding.ASCII.GetBytes(s);
if (p.Length > d.Length) return false;
for (int i = 0; i <= d.Length - p.Length; i++)
{
bool ok = true;
for (int j = 0; j < p.Length; j++)
if (d[i + j] != p[j]) { ok = false; break; }
if (ok) return true;
}
return false;
}
static string CheckZip(string path)
{
try
{
using (ZipArchive za = ZipFile.OpenRead(path))
{
foreach (ZipArchiveEntry e in za.Entries)
{
string n = e.FullName.ToLowerInvariant();
if (n.EndsWith("/appxmanifest.xml") || n.Equals("appxmanifest.xml"))
{
using (StreamReader sr = new StreamReader(e.Open()))
{
string xml = sr.ReadToEnd();
int i = xml.IndexOf("ProcessorArchitecture", StringComparison.OrdinalIgnoreCase);
if (i >= 0)
{
int q = xml.IndexOf('"', i);
int q2 = q >= 0 ? xml.IndexOf('"', q + 1) : -1;
if (q2 > q)
{
string pa = xml.Substring(q + 1, q2 - q - 1).Trim().ToLowerInvariant();
if (pa == "x64") return "64 位安装包 (APPX/MSIX, x64)";
if (pa == "x86") return "32 位安装包 (APPX/MSIX, x86)";
if (pa == "arm64") return "64 位安装包 (APPX/MSIX, ARM64)";
if (pa == "neutral") return "中性安装包 (APPX/MSIX, 任意架构)";
return "APPX/MSIX 架构: " + pa;
}
}
return "APPX/MSIX:未在清单中找到架构声明";
}
}
}
foreach (ZipArchiveEntry e in za.Entries)
{
string n = e.FullName.ToLowerInvariant();
if (!(n.EndsWith(".exe") || n.EndsWith(".dll") || n.EndsWith(".sys") || n.EndsWith(".ocx"))) continue;
using (Stream st = e.Open())
{
byte[] h = new byte[4096];
int r = st.Read(h, 0, h.Length);
string m = PeMachine(h, r);
if (m != null) return m + " (压缩包内: " + e.FullName + ")";
}
}
return "压缩包/安装包:未发现内嵌 PE 组件,可能是数据包";
}
}
catch (InvalidDataException) { return "ZIP 文件损坏"; }
catch (Exception ex) { return "ZIP 读取失败: " + ex.Message; }
}
static string ResolveLnk(string path)
{
try
{
Type t = Type.GetTypeFromProgID("WScript.Shell");
if (t == null) return null;
object sh = Activator.CreateInstance(t);
object lnk = t.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, sh, new object[] { path });
return (string)t.InvokeMember("TargetPath", BindingFlags.GetProperty, null, lnk, null);
}
catch { return null; }
}
}
class MainForm : Form
{
private RichTextBox log;
public MainForm()
{
Text = "BitChecker - 拖入文件判断 32/64 位";
ClientSize = new Size(640, 420);
MinimumSize = new Size(480, 300);
StartPosition = FormStartPosition.CenterScreen;
AllowDrop = true;
BackColor = Color.FromArgb(24, 26, 36);
Font = new Font("Microsoft YaHei UI", 10F);
log = new RichTextBox();
log.Dock = DockStyle.Fill;
log.ReadOnly = true;
log.BorderStyle = BorderStyle.None;
log.BackColor = Color.FromArgb(20, 22, 32);
log.ForeColor = Color.FromArgb(230, 230, 240);
log.Font = new Font("Consolas", 10.5F);
log.DetectUrls = false;
FlowLayoutPanel top = new FlowLayoutPanel();
top.Dock = DockStyle.Top;
top.Height = 52;
top.Padding = new Padding(14, 10, 8, 8);
top.BackColor = Color.FromArgb(34, 38, 52);
Label tip = new Label();
tip.Text = "把 安装包 / exe / dll / msi / msix 拖进窗口即可自动判断";
tip.AutoSize = false;
tip.Width = 390;
tip.Height = 30;
tip.TextAlign = ContentAlignment.MiddleLeft;
tip.ForeColor = Color.FromArgb(190, 200, 220);
tip.Margin = new Padding(0, 0, 10, 0);
Button btnOpen = MakeButton("浏览文件...", 100);
btnOpen.Click += delegate { OpenFiles(); };
Button btnClear = MakeButton("清空", 56);
btnClear.Click += delegate { log.Clear(); };
top.Controls.Add(tip);
top.Controls.Add(btnOpen);
top.Controls.Add(btnClear);
Controls.Add(log);
Controls.Add(top);
DragEnter += OnDragEnter;
DragDrop += OnDragDrop;
}
private Button MakeButton(string text, int width)
{
Button b = new Button();
b.Text = text;
b.AutoSize = false;
b.Size = new Size(width, 30);
b.FlatStyle = FlatStyle.Flat;
b.FlatAppearance.BorderColor = Color.FromArgb(90, 100, 130);
b.ForeColor = Color.FromArgb(220, 225, 235);
b.BackColor = Color.FromArgb(48, 54, 74);
b.Margin = new Padding(0, 0, 8, 0);
return b;
}
private void OnDragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private void OnDragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (files == null) return;
foreach (string f in files) AddResult(f);
}
private void OpenFiles()
{
using (OpenFileDialog dlg = new OpenFileDialog())
{
dlg.Multiselect = true;
dlg.Title = "选择要检测的文件";
if (dlg.ShowDialog(this) == DialogResult.OK)
foreach (string f in dlg.FileNames) AddResult(f);
}
}
private void AddResult(string path)
{
string r = Detector.Describe(path);
Color c;
if (r.Contains("64 位")) c = Color.FromArgb(110, 200, 255);
else if (r.Contains("32 位")) c = Color.FromArgb(255, 190, 110);
else if (r.Contains("通用")) c = Color.FromArgb(150, 220, 150);
else c = Color.FromArgb(255, 120, 120);
log.SelectionStart = log.TextLength;
log.SelectionColor = Color.FromArgb(150, 155, 170);
log.AppendText(Path.GetFileName(path) + Environment.NewLine);
log.SelectionColor = c;
log.AppendText(" " + r + Environment.NewLine + Environment.NewLine);
log.SelectionColor = log.ForeColor;
log.ScrollToCaret();
}
}
static class Program
{
[DllImport("kernel32.dll")]
static extern IntPtr GetStdHandle(int nStdHandle);
static void WriteConsole(string text)
{
try
{
IntPtr h = GetStdHandle(-11);
if (h == IntPtr.Zero || h == new IntPtr(-1)) return;
byte[] bytes = Encoding.UTF8.GetBytes(text);
using (FileStream fs = new FileStream(new SafeFileHandle(h, false), FileAccess.Write))
{
fs.Write(bytes, 0, bytes.Length);
}
}
catch { }
}
[STAThread]
static void Main(string[] args)
{
if (args.Length > 0)
{
StringBuilder sb = new StringBuilder();
foreach (string a in args)
sb.AppendLine(a + "\t" + Detector.Describe(a));
WriteConsole(sb.ToString());
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}