/// <summary> /// Gets information about the assembly. /// See comments in UninstallAssembly for specifying the assembly name. /// </summary> /// <param name="assemblyName"></param> /// <returns></returns> private static string QueryAssemblyInfo(String assemblyName) { if (assemblyName == null) { throw new ArgumentException("Invalid name", "assemblyName"); } AssemblyInfo aInfo = new AssemblyInfo(); aInfo.cchBuf = 1024; // Get a string with the desired length aInfo.currentAssemblyPath = new string('\0', aInfo.cchBuf); IAssemblyCache ac = null; int hr = GacUtils.CreateAssemblyCache(out ac, 0); if (hr >= 0) { hr = ac.QueryAssemblyInfo((int)QueryAssemblyInfoFlags.DEFAULT, assemblyName, ref aInfo); } if (hr == -2147024894) //File not found - This is a common error which need not be represented as an exception(ruins debug experience, also). { return(null); } else if (hr < 0) { GacUtils.ThrowMarshalledException(hr); } return(aInfo.currentAssemblyPath); }
/// <summary> /// Installs an assembly into the GAC. Adds a reference to the installing applicaiton. /// </summary> /// <param name="assemblyPath">The path to the assembly to be installed</param> /// <param name="flags">see AssemblyCommitFlags</param> internal static void InstallAssembly(String assemblyPath, AssemblyCommitFlags flags) { IAssemblyCache ac = null; int hr = 0; hr = GacUtils.CreateAssemblyCache(out ac, 0); if (hr >= 0) { hr = ac.InstallAssembly((int)flags, assemblyPath, null); } if (hr < 0) { GacUtils.ThrowMarshalledException(hr); } }
/// <summary> /// Uninstalls the assembly, using the application as a reference. /// AssemblyName has to be fully specified name. /// A.k.a, for v1.0/v1.1 assemblies, it should be "name, Version=xx, Culture=xx, PublicKeyToken=xx". /// For v2.0 assemblies, it should be "name, Version=xx, Culture=xx, PublicKeyToken=xx, ProcessorArchitecture=xx". /// If assemblyName is not fully specified, a random matching assembly will be uninstalled. /// </summary> /// <param name="assemblyName"></param> private static AssemblyCacheUninstallDisposition UninstallAssemblyImplementation(String assemblyName) { AssemblyCacheUninstallDisposition dispResult = AssemblyCacheUninstallDisposition.Uninstalled; IAssemblyCache ac = null; int hr = GacUtils.CreateAssemblyCache(out ac, 0); if (hr >= 0) { hr = ac.UninstallAssembly(0, assemblyName, null, out dispResult); } if (hr < 0) { GacUtils.ThrowMarshalledException(hr); } return(dispResult); }