要获取每个GAC(全局程序集缓存)程序集的位置,您可以使用.NET Framework提供的工具,如gacutil.exe
,或者使用代码
方法1:使用gacutil.exe
工具
方法2:使用代码
以下是一个简单的C#控制台应用程序,用于检索GAC中程序集的位置。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace GacAssemblyLocator
{
class Program
{
static void Main(string[] args)
{
Assembly[] gacAssemblies = GetGacAssemblies();
foreach (Assembly assembly in gacAssemblies)
{
Console.WriteLine($"Name: {assembly.FullName}, Location: {assembly.Location}");
}
Console.ReadLine();
}
private static Assembly[] GetGacAssemblies()
{
string gacPath = @"C:\Windows\assembly\";
var directory = new DirectoryInfo(gacPath);
List<Assembly> assemblies = new List<Assembly>();
foreach (var pkg in directory.GetDirectories())
{
var assemblyDirs = pkg.GetDirectories("GAC_MSIL");
if (assemblyDirs.Any())
{
foreach (var assemblyDir in assemblyDirs)
{
var assemblyFiles = assemblyDir.GetFiles("*.dll");
if (assemblyFiles.Any())
{
foreach (var assemblyFile in assemblyFiles)
{
try
{
Assembly assembly = Assembly.LoadFrom(assemblyFile.FullName);
assemblies.Add(assembly);
}
catch (Exception ex)
{
Console.WriteLine($"Unable to load assembly {assemblyFile.FullName}: {ex.Message}");
}
}
}
}
}
}
return assemblies.ToArray();
}
}
}
这段代码首先获取GAC的程序集路径,然后遍历所有程序集,尝试加载它们,将找到的程序集添加到结果列表中,并在控制台上输出相关信息。
领取专属 10元无门槛券
手把手带您无忧上云