C# Source Code: Programmatically adding and removing assemblies from the GAC (without using GACUTIL)
[
Home
|
Contents
|
Search
|
Reply
| Previous |
Next
]
C# Source Code
Programmatically adding and removing assemblies from the GAC (without using GACUTIL)
By:
Andrew Baker
Email (spam proof):
Email the originator of this post
Date:
Thursday, November 03, 2005
Hits:
4890
Category:
General/Framework
Article:
The C# code below demonstrates how to programmatically add and remove items from the Global Aseembly Cache (GAC). using System; using System.IO; using System.Runtime.InteropServices; #region AssemblyCommitFlags Enum ///
/// Enum which descibes how to add the assembly to the GAC. ///
public enum AssemblyCommitFlags { ///
/// Default installation. ///
Default = 1, ///
/// Forces reinstall of an assembly regardless of any existing assembly with the same assembly name. ///
Force = 2 } #endregion AssemblyCommitFlags Enum #region AssemblyCacheUninstall Enum ///
/// Enum to return the status of an GAC uninstall request. ///
public enum AssemblyCacheUninstall: int { ///
/// Unknow error. ///
Unknown = 0, ///
/// The assembly files have been removed from the GAC. ///
Uninstalled = 1, ///
/// An application is using the assembly. /// This value is returned on Microsoft Windows 95 and Microsoft Windows 98. ///
StillInUse = 2, ///
/// The assembly does not exist in the GAC. ///
AlreadyUninstalled = 3, ///
/// Not used. ///
DeletePending = 4, ///
/// The assembly has not been removed from the GAC because another application reference exists. ///
HasInstallReference = 5, ///
/// The reference that is specified in pRefData is not found in the GAC. The return values are defined as follows: /// S_OK - The assembly has been uninstalled. /// S_FALSE - The operation succeeded, but the assembly was not removed from the GAC. The reason is described in pulDisposition. ///
ReferenceNotFound = 6 } #endregion AssemblyCacheUninstall Enum #region IAssemblyCache Interface ///
/// Represents the Global Assembly Cache for use by Fusion. ///
[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("e707dcde-d1cd-11d2-bab9-00c04f8eceae")] internal interface IAssemblyCache { ///
/// Uninstalls the specified assembly from the Global Assembly Cache. ///
[PreserveSig()] int UninstallAssembly(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssemblyName, IntPtr pvReserved, out AssemblyCacheUninstall pulDisposition); ///
/// Gets the requested data about the specified assembly. ///
[PreserveSig()] int QueryAssemblyInfo(uint dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszAssemblyName, IntPtr pAsmInfo); ///
/// Gets a new IAssemblyCacheItem ///
[PreserveSig()] int CreateAssemblyCacheItem(uint dwFlags, IntPtr pvReserved, out /*IAssemblyCacheItem*/IntPtr ppAsmItem, [MarshalAs(UnmanagedType.LPWStr)] String pszAssemblyName); ///
/// Reserved for internal use by fusion. ///
[PreserveSig()] int CreateAssemblyScavenger(out object ppAsmScavenger); ///
/// Installs the specified assembly into the Global Assembly Cache. ///
[PreserveSig()] int InstallAssembly(int dwFlags, [MarshalAs(UnmanagedType.LPWStr)] string pszManifestFilePath, IntPtr pvReserved); } #endregion IAssemblyCache Interface #region FusionHelper Public Class ///
/// Helper class for adding and removing assemblies to and from the GAC. ///
public class FusionHelper { #region Public Constants private const int MAX_PATH = 260; #endregion Public Constants #region API Calls [DllImport("Fusion.dll", CharSet=CharSet.Auto)] internal static extern int CreateAssemblyCache(out IAssemblyCache ppAsmCache, uint dwReserved); #endregion API Calls #region Public Methods ///
/// Adds an assembly to the GAC. ///
///
The path and file name of the assembly to add. ///
The type of installing to perform. ///
Returns true on success, else false.
public static bool AddAssemblyToCache(string assembly, AssemblyCommitFlags commitType) { IAssemblyCache ac = null; // Create a assembly cache item int hr = CreateAssemblyCache(out ac, 0); if (hr != 0) { return false; } else { // Install assembly hr = ac.InstallAssembly( (int)commitType, assembly, (IntPtr)0); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } // Success return true; } } ///
/// Removes an assembly from the GAC. ///
///
The fully specified assembly name of the format [AssemblyName], [Version], [Culture]. ///
The result of the uninstall request. ///
Returns true on success, else false.
///
Fusion.RemoveAssemblyFromCache("MyAssembly.dll, 1.0.0.0, Neutral", out result);
public static bool RemoveAssemblyFromCache(string assembly, out AssemblyCacheUninstall result) { IAssemblyCache ac = null; // Create a assembly cache item int hr = CreateAssemblyCache(out ac, 0); result = AssemblyCacheUninstall.Unknown; if (hr != 0) { // Failed return false; } else { // Uninstall assembly hr = ac.UninstallAssembly(0, assembly, (IntPtr)0, out result); if (hr < 0) { Marshal.ThrowExceptionForHR(hr); } return true; } } #endregion Public Methods } #endregion FusionHelper Public Class
Terms and Conditions
Support this site
Download a trial version of the best FTP application on the internet