Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
232 lines (194 loc) · 6.31 KB
/
Copy pathProgram.cs
File metadata and controls
232 lines (194 loc) · 6.31 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
using System;
using System.Collections.Generic;
using System.IO;
#if NET35_OR_GREATER || NETSTANDARD || NETCOREAPP
using System.Linq;
#endif
using System.Reflection;
#if NET461_OR_GREATER || NETSTANDARD || NETCOREAPP
using System.Reflection.Metadata;
using System.Reflection.Metadata.Ecma335;
using System.Reflection.PortableExecutable;
#endif
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
namespace netcheck;
#pragma warning disable IDE0079 // Remove unnecessary suppression
#pragma warning disable IDE0057 // Use range operator
public static class Program
{
private static readonly Version Version_5_0 = new(5, 0);
public static int Main(params string[] args)
{
try
{
return UnsafeMain(args);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine($"Exception: {ex}");
Console.ResetColor();
return Marshal.GetHRForException(ex);
}
}
public static int UnsafeMain(params string[] args)
{
var errors = 0;
var nodep = false;
var depforall = false;
foreach (var arg in args)
{
if (arg == "-l")
{
nodep = true;
continue;
}
if (arg == "-a")
{
depforall = true;
continue;
}
try
{
var path = Path.GetFullPath(arg);
var asmname = AssemblyName.GetAssemblyName(path);
DisplayDependencies([], Path.GetDirectoryName(path), asmname, "", nodep, depforall);
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.Write(ex.ToString());
Console.Error.Write(": ");
Console.Error.WriteLine(arg);
Console.ResetColor();
errors += 1;
}
}
return errors;
}
public static void DisplayDependencies(List<AssemblyName> asmlist, string? basepath, AssemblyName asmname, string indentlevel, bool nodep, bool depforall)
{
var existing = asmlist.Find(name => AssemblyName.ReferenceMatchesDefinition(name, asmname)) is not null;
if (!depforall && existing)
{
return;
}
if (!existing)
{
asmlist.Add(asmname);
}
Assembly? asm = null;
try
{
if (asmname.CodeBase is not null)
{
asm = Assembly.LoadFrom(asmname.CodeBase);
}
else
{
asm = Assembly.Load(asmname);
}
}
catch
{
}
if (asm is null)
{
try
{
var dllName = $"{asmname.Name}.dll";
var path = Path.Combine(basepath ?? ".", dllName);
if (!File.Exists(path))
{
path = Path.Combine(RuntimeEnvironment.GetRuntimeDirectory(), dllName);
}
asm = Assembly.LoadFrom(path);
asmname = asm.GetName();
}
catch (Exception ex)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Error.WriteLine($"Error loading {asmname}: {ex.GetBaseException().Message}");
Console.ResetColor();
return;
}
}
var isFrameworkAssembly = false;
#if !NETCOREAPP
isFrameworkAssembly = asm.GlobalAssemblyCache;
#else
isFrameworkAssembly = Path.GetDirectoryName(asm.Location.AsSpan()).Equals(RuntimeEnvironment.GetRuntimeDirectory().AsSpan().TrimEnd(Path.DirectorySeparatorChar), StringComparison.OrdinalIgnoreCase);
#endif
if (existing && isFrameworkAssembly)
{
return;
}
Console.Write(indentlevel);
if (isFrameworkAssembly)
{
Console.ForegroundColor = ConsoleColor.Green;
}
else
{
Console.ForegroundColor = ConsoleColor.White;
Console.Write($"{asm.Location}: ");
}
Console.Write(asmname.FullName);
#if NET461_OR_GREATER || NETSTANDARD || NETCOREAPP
try
{
using var reader = new PEReader(asm.GetFiles()[0]);
var metadataversion = reader.GetMetadataReader().MetadataVersion;
var target_framework = asm.GetCustomAttributes<TargetFrameworkAttribute>().FirstOrDefault()?.FrameworkName;
var framework = GetTargetFramework(metadataversion, target_framework);
if (!string.IsNullOrWhiteSpace(framework))
{
Console.Write(", ");
Console.Write(framework);
}
}
catch
{
}
#endif
Console.ResetColor();
Console.WriteLine();
if (nodep || existing)
{
return;
}
var subindentlevel = $"{indentlevel} ";
foreach (var refasm in asm.GetReferencedAssemblies())
{
DisplayDependencies(asmlist, basepath, refasm, subindentlevel, nodep, depforall);
}
}
#if NET461_OR_GREATER || NETSTANDARD || NETCOREAPP
private static string? GetTargetFramework(string metadataversion, string? target_framework)
{
if (string.IsNullOrWhiteSpace(metadataversion))
{
return null;
}
if (target_framework is null
|| string.IsNullOrWhiteSpace(target_framework))
{
var netfx = metadataversion.Split(['v', '.']);
return $"net{netfx[1]}{netfx[2]}";
}
if (target_framework.StartsWith(".NETFramework,Version=v", StringComparison.Ordinal))
{
return $"net{target_framework.Substring(".NETFramework,Version=v".Length).Replace(".", "")}";
}
var sep = target_framework.IndexOf(",Version=v", StringComparison.Ordinal);
var fx = target_framework.Remove(sep).TrimStart('.').ToLowerInvariant();
var ver = target_framework.Substring(sep + ",Version=v".Length);
if (fx == "netcoreapp" && Version.Parse(ver) >= Version_5_0)
{
fx = "net";
}
return $"{fx}{ver}";
}
#endif
}