从GAC卸载的C#代码 [英] Uninstall from GAC In C# code

查看:201
本文介绍了从GAC卸载的C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何从我的C#应用​​程序卸载GAC。



我无法卸载,从GAC特定EXE和DLL。



是否要卸载GAC在C#中的正确方法?

 公共无效RemoveAssembly(串ShortAssemblyName,串PublicToken)
{
AssemblyCacheEnum AssembCache =新AssemblyCacheEnum(NULL);

字符串FullAssembName = NULL;

为(;)
{
串AssembNameLoc = AssembCache.GetNextAssembly();
如果(AssembNameLoc == NULL)
中断;

串铂;
串SHORTNAME = GetAssemblyShortName(AssembNameLoc,出铂);

如果(ShortAssemblyName == SHORTNAME)如果
{

(PublicToken!= NULL)
{
PublicToken = PublicToken.Trim( )。降低();
如果(PT == NULL)
{
FullAssembName = AssembNameLoc;
中断;
}

PT = Pt.ToLower()修剪();

如果(PublicToken ==铂)
{
FullAssembName = AssembNameLoc;
中断;
}
}
,否则
{
FullAssembName = AssembNameLoc;
中断;
}
}
}

串Stoken =空;
如果(PublicToken!= NULL)
{
Stoken = PublicToken;
}

如果(FullAssembName == NULL)
抛出新的异常(集结号=+ ShortAssemblyName +,PublicToken =+
+令牌未找到在GAC);

AssemblyCacheUninstallDisposition UninstDisp;


AssemblyCache.UninstallAssembly(FullAssembName,空,出UninstDisp);
}


公共静态无效UninstallAssembly(字符串的AssemblyName,InstallReference参考,走出AssemblyCacheUninstallDisposition DISP)
{
AssemblyCacheUninstallDisposition dispResult = AssemblyCacheUninstallDisposition.Uninstalled;
如果(参考!= NULL)
{
如果(!InstallReferenceGuid.IsValidGuidScheme(reference.GuidScheme))
抛出新的ArgumentException(无效参考GUID。,GUID );
}

IAssemblyCache AC = NULL;

INT HR = Utils.CreateAssemblyCache(出AC,0);
如果(HR> = 0)
{
HR = ac.UninstallAssembly(0,的AssemblyName,引用了dispResult);
}

如果(小时℃的)
{
Marshal.ThrowExceptionForHR(小时);
}

DISP = dispResult;
}


解决方案

我们建议你不要做到这一点,因为这可能会导致使用该DLL的问题到其他应用程序。但我以为这是你自己的,所以它是比较安全的。



使用下面的代码从您的GAC删除DLL。该asssembly名称是您的DLL没有扩展名。

 公共静态无效UninstallAssembly(字符串的AssemblyName){
的ProcessStartInfo的ProcessStartInfo =新的ProcessStartInfo(的Gacutil.exe,
的String.Format(/ U {0} .dll文件的AssemblyName));

processStartInfo.UseShellExecute = FALSE;
工艺过程=的Process.Start(的ProcessStartInfo);
process.WaitForExit;
}


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

解决方案

You're advised to not do it, as it can cause problems to other applications using that dll. But I assume it's your own, so it is relatively safe.

Use the next code to remove the dll from your GAC. The asssembly name is the name of your dll without the extension.

public static void UninstallAssembly(string assemblyName) {
   ProcessStartInfo processStartInfo = new ProcessStartInfo("gacutil.exe", 
      string.Format("/u {0}.dll", assemblyName));

   processStartInfo.UseShellExecute = false;
   Process process = Process.Start(processStartInfo);
   process.WaitForExit;
}

这篇关于从GAC卸载的C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆