• 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;
    }
    

      

  • 相关阅读:
    第二阶段冲刺第五天(6月4号)
    第二阶段冲刺第四天(6月3号)
    第二次阶段冲刺第三天(6月2号)
    第二次阶段冲刺第二天(6月1号)
    第二次阶段冲刺第一天(5月31号)
    第十周学习进度
    第十一周学习进度
    第十二周学习进度
    javascript 将递归转化为循环
    创建数据库,并设置外部访问权限
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/4898008.html
Copyright © 2020-2023  润新知