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(100);
            new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
            Thread.Sleep(100);
            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(100);
            new System.EnterpriseServices.Internal.Publish().GacInstall(dllPath);
            Thread.Sleep(100);
            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);
原文地址:https://www.cnblogs.com/nanfei/p/7458253.html