zoukankan      html  css  js  c++  java
  • Uninstall from GAC In C# code

    How do I uninstall the GAC from my C# application.

    I am not able to uninstall, the particular exe and DLL from GAC.

    Is it the proper way to uninstall the GAC in C# ?

    public void RemoveAssembly(string ShortAssemblyName, string PublicToken)
    {
        AssemblyCacheEnum AssembCache = new AssemblyCacheEnum(null);
    
        string FullAssembName = null;
    
        for (; ; )
        {
            string AssembNameLoc = AssembCache.GetNextAssembly();
            if (AssembNameLoc == null)
                break;
    
            string Pt;
            string ShortName = GetAssemblyShortName(AssembNameLoc, out Pt);
    
            if (ShortAssemblyName == ShortName)
            {
    
                if (PublicToken != null)
                {
                    PublicToken = PublicToken.Trim().ToLower();
                    if (Pt == null)
                    {
                        FullAssembName = AssembNameLoc;
                        break;
                    }
    
                    Pt = Pt.ToLower().Trim();
    
                    if (PublicToken == Pt)
                    {
                        FullAssembName = AssembNameLoc;
                        break;
                    }
                }
                else
                {
                    FullAssembName = AssembNameLoc;
                    break;
                }
            }
        }
    
        string Stoken = "null";
        if (PublicToken != null)
        {
            Stoken = PublicToken;
        }
    
        if (FullAssembName == null)
            throw new Exception("Assembly=" + ShortAssemblyName + ",PublicToken=" + 
            token + " not found in GAC");
    
        AssemblyCacheUninstallDisposition UninstDisp;
    
    
        AssemblyCache.UninstallAssembly(FullAssembName, null, out UninstDisp);
    }
    
    
    public static void UninstallAssembly(String assemblyName, InstallReference reference, out AssemblyCacheUninstallDisposition disp)
    {
        AssemblyCacheUninstallDisposition dispResult = AssemblyCacheUninstallDisposition.Uninstalled;
        if (reference != null)
        {
            if (!InstallReferenceGuid.IsValidGuidScheme(reference.GuidScheme))
                throw new ArgumentException("Invalid reference guid.", "guid");
        }
    
        IAssemblyCache ac = null;
    
        int hr = Utils.CreateAssemblyCache(out ac, 0);
        if (hr >= 0)
        {
            hr = ac.UninstallAssembly(0, assemblyName, reference, out dispResult);
        }
    
        if (hr < 0)
        {
            Marshal.ThrowExceptionForHR(hr);
        }
    
        disp = dispResult;
    }
    

      

  • 相关阅读:
    mybatis
    BeanUtil拷贝
    lombok(@Getter&@Setter)
    fly插件飞向购物车
    原生JavaScript判断是否为邮箱、危险字符、验证长度、验证网址、验证小数、整数、浮点数等常用的 js 验证
    原生JavaScript获取复选框的值
    原生JavaScript获取单选按钮的值
    原生JavaScript实现返回顶部的通用方法
    原生JavaScript获得URL中GET参数值
    原生JavaScript常用的正则表达式
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/4898008.html
Copyright © 2011-2022 走看看