Skip to content

Instantly share code, notes, and snippets.

@xcud
Created February 5, 2013 12:58
Show Gist options
  • Save xcud/4714300 to your computer and use it in GitHub Desktop.
Save xcud/4714300 to your computer and use it in GitHub Desktop.
A simple wrapper for GAC.cs from Microsoft KB Article KB317540
// System.GAC (a .Net wrapper for Fusion) implementation was too messy. Simplified here.
//
// Note, installing an assembly with the REFRESH flag specified does NOT work as advertised.
// Overwriting old GAC'd assemblies must be done manually as implemented below.
//
using System.Reflection;
using System.Text;
namespace Xcud.System.GAC
{
public static class Simplified
{
public static void Install(string assemblyName, string path)
{
System.EnterpriseServices.Internal.Publish publisher = new System.EnterpriseServices.Internal.Publish();
AssemblyName asm = AssemblyName.GetAssemblyName(path);
bool installToGAC = true;
IAssemblyEnum enumerator = System.GAC.AssemblyCache.CreateGACEnum();
while (true)
{
IAssemblyName name = null;
enumerator.GetNextAssembly(IntPtr.Zero, out name, 0);
if (name == null)
break;
uint len = 1024;
StringBuilder sb = new StringBuilder((int)len);
name.GetName(ref len, sb);
if (sb.Length == 0)
break;
if (!string.IsNullOrEmpty(name) && sb.ToString().ToLower().StartsWith(assemblyName.ToLower()))
{
len = 1024;
StringBuilder displayName = new StringBuilder((int)len);
name.GetDisplayName(displayName, ref len, 0);
// uninstall it
string location = Assembly.ReflectionOnlyLoad(displayName.ToString()).Location;
publisher.GacRemove(location);
}
if (sb.ToString().ToLower().StartsWith(asm.Name.ToLower()))
{
len = 1024;
StringBuilder displayName = new StringBuilder((int)len);
name.GetDisplayName(displayName, ref len, 0);
string asmPath = Assembly.ReflectionOnlyLoad(displayName.ToString()).Location;
int result = -1;
try
{
Version foundVersion = AssemblyName.GetAssemblyName(asmPath).Version;
result = foundVersion.CompareTo(asm.Version);
}
catch(Exception) { }
if (result < 0) // found version is older
{
// uninstall it
string location = Assembly.ReflectionOnlyLoad(displayName.ToString()).Location;
publisher.GacRemove(location);
}
else if (result == 0) // found version is the same
{
string location = Assembly.ReflectionOnlyLoad(displayName.ToString()).Location;
publisher.GacRemove(location);
}
else if (result > 0) // found version is newer
{
installToGAC = false;
}
}
}
if (installToGAC)
publisher.GacInstall(path);
}
public static void Remove(string assemblyName)
{
System.EnterpriseServices.Internal.Publish publisher = new System.EnterpriseServices.Internal.Publish();
IAssemblyEnum enumerator = System.GAC.AssemblyCache.CreateGACEnum();
while (true)
{
IAssemblyName name = null;
enumerator.GetNextAssembly(IntPtr.Zero, out name, 0);
if (name == null)
break;
uint len = 1024;
StringBuilder sb = new StringBuilder((int)len);
name.GetName(ref len, sb);
if (sb.Length == 0)
break;
if sb.ToString().ToLower().StartsWith(assemblyName.ToLower()))
{
len = 1024;
StringBuilder displayName = new StringBuilder((int)len);
name.GetDisplayName(displayName, ref len, 0);
// uninstall it
string location = Assembly.ReflectionOnlyLoad(displayName.ToString()).Location;
publisher.GacRemove(location);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment