Reputation: 242
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;
}
Upvotes: 12
Views: 4943
Reputation: 3909
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;
}
Upvotes: 1
Reputation: 691
You can see the .NET reference code for details on how it can done programmatically (although the license for the reference site will restrict you from using it in your own software, i.e., copy-pasting this code is not a good idea).
http://referencesource.microsoft.com/#System.Web/xsp/system/Web/Configuration/GacUtil.cs
(scroll down to public bool GacUnInstall(string assemblyName))
See also IAssemblyCache
http://referencesource.microsoft.com/#System.Web/xsp/system/Web/Configuration/IAssemblyCache.cs
and the native methods for GetAssemblyCache
http://referencesource.microsoft.com/#System.Web/xsp/system/Web/NativeMethods.cs
On the other hand, since the code is in assembly System.Web and the class is marked as internal, you can use reflection to access it. See
How to access internal class using Reflection
Upvotes: 0
Reputation: 2196
Assuming the DLLs are in the same directory as where this code is running from, try:
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);
}
Hope this helps someone...
Upvotes: 2
Reputation: 2037
Why don't you just call "gacutil /u " from the application? Or am I missing something?
Upvotes: 0
Reputation: 335
How to install an assembly into the Global Assembly Cache in Visual C#:
Here is the solution from Microsoft support article: http://support.microsoft.com/kb/815808
Another link(registering, unregistering libraries from GAC): http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/7d3165cf-ca3b-43cc-8f77-a46dbf38f13d/
Upvotes: 0
Reputation: 57
Follow this steps :-
using System;
using System.Reflection;
private string assemblyFullPath = "";
public class myAssembly
{
static void Main()
{
Assembly assembly = Assembly.Load("library, version=1.0.0.0, culture=neutral, PublicKeyToken=9b184fc90fb9648d");
assemblyFullPath = assembly.Location;
}
}
You can also load the assembly file with LoadFrom() method of Assembly class but it is not good to use it, you can read the post hereto know the reasons.
Once we have the assemblyFullPath, it is easy to uninstall/ remove the assembly from GAC. The System.EnterpriseServices.Internal namespace provides methods to install or remove GAC files. Follow the below steps to do install/remove the files:
Now with the assemblyFullPath, add the below lines of code in your method where you want to remove the assembly files.
Publish publishProcess = new Publish(); publishProcess.GacRemove(assemblyFullPath);
Hope It will help you
Upvotes: 1
Reputation: 1653
Why you want to do that? GAC is the Global Assembly Cache, basicly is the system folder that contains your PC's Shared DLLs.
If you want to remove the assembly anyway check the gacutil documentation here: http://msdn.microsoft.com/en-us/library/ex0ss12c(v=vs.71).aspx But it will be removed for all your projects!
Upvotes: 1