C#通过代码判断并注册程序集到GAC

时间:2023-03-09 14:31:43
C#通过代码判断并注册程序集到GAC
            var dllName = "EasyHook.dll";
var dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
if (System.Runtime.InteropServices.RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
new System.EnterpriseServices.Internal.Publish().GacRemove(dllPath);
Thread.Sleep();
new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
Thread.Sleep();
if (System.Runtime.InteropServices.RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
Console.WriteLine("{0} registered to GAC successfully.", dllName);
else
Console.WriteLine("{0} registered to GAC failed.", dllName); dllName = "ComplexParameterInject.dll";
dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, dllName);
if (System.Runtime.InteropServices.RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
new System.EnterpriseServices.Internal.Publish().GacRemove(dllPath);
Thread.Sleep();
new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
Thread.Sleep();
if (System.Runtime.InteropServices.RuntimeEnvironment.FromGlobalAccessCache(Assembly.LoadFrom(dllPath)))
Console.WriteLine("{0} registered to GAC successfully.", dllName);
else
Console.WriteLine("{0} registered to GAC failed.", dllName);