gpt4 book ai didi

c# - 从 GAC 在 C# 代码中卸载

转载 作者:可可西里 更新时间:2023-11-01 08:28:16 26 4
gpt4 key购买 nike

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

我无法从 GAC 卸载特定的 exe 和 DLL。

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

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

最佳答案

假设 DLL 与运行此代码的目录位于同一目录中,请尝试:

ArrayList libraries = new ArrayList();
libraries.Add("somedll1.dll");
libraries.Add("somedll2.dll");

Console.WriteLine("Registering DDLs in the Gac");

try
{
for (int i = 0; i < libraries.Count; i++)
{
// get the path from the running exe, and remove the running exe's name from it
new System.EnterpriseServices.Internal.Publish().GacRemove(System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("ThisUtilitiesName.exe", "") + libraries[i]);
}

Console.WriteLine("Completed task successfully");
}
catch (Exception exp)
{
Console.WriteLine(exp.Message);
}

希望这对某人有帮助...

关于c# - 从 GAC 在 C# 代码中卸载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11117043/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com