Jan Schreuder on .Net

.Net code samples, experiences, observations

View my professional profile on LinkedIn

Recent Posts

Tags

News

  • Inappropriate comments will be deleted at my discretion.

    The information and code samples in this weblog is provided "AS IS" without warranty of any kind, either expressed or implied, including but not limited to the merchantability and/or fitness for a particular purpose.

Community

Email Notifications

Tool suppliers

Tools

General

Microsoft

Favorite blogs

Archives

Getting a copy of a DLL in the GAC

A few weeks ago, Patrick Wellink blogged about how you would get a copy of a DLL when it's in the GAC. Today I had the same problem but after reading Patrick's blog post and the comments, I decided to write a small console application to make that process somewhat easier.

This application takes at least two arguments. The first argument is the physical path to the GAC on the system. The second is the path where the DLL needs to be copied. The following example copies all Crystal components in the GAC to a backup folder:

GetGACAssemblies C:\Windows\Assembly C:\Projects\GACBackup Crystal*.dll

If you just need a backup of all GAC assemblies, simply do something like this:

GetGACAssemblies C:\Windows\Assembly C:\Projects\GACBackup

To build the application, I used the ScanDirectory class I blogged about earlier. The code for this console application is really simple:

using System;
using System.IO;
 
namespace GetGACAssemblies
{
    class Program
    {
        private static string _targetPath;
 
        static void Main(string[] args)
        {
            // You need at least two arguments
            if (args.Length >= 2)
            {
                // Get the target path and make sure it exists
                _targetPath = args[1];
                if (!_targetPath.EndsWith(@"\"))
                {
                    _targetPath += @"\";
                }
 
                if (!Directory.Exists(_targetPath))
                {
                    Directory.CreateDirectory(_targetPath);
                }
 
                // Setup the directory scanner object
                ScanDirectory scanner = new ScanDirectory();
 
                scanner.FileEvent += new ScanDirectory.FileEventHandler(scanner_FileEvent);
                if (args.Length == 2)
                {
                    scanner.SearchPattern = "*.dll";
                }
                else
                {
                    scanner.SearchPattern = args[2];
                }
 
                // Start the scan
                scanner.WalkDirectory(args[0]);
            }
        }
 
        static void scanner_FileEvent(object sender, FileEventArgs e)
        {
            string newFile = _targetPath + e.Info.Name;
 
            if (File.Exists(newFile))
            {
                File.Delete(newFile);
            }
            File.Copy(e.Info.FullName, newFile);
        }
    }
}

You can download the code for the console application here.

Comments

Ramon Smits said:

The assembly directory is probably always %WINDIR%\Assembly so you could use that environment variable.
# August 9, 2006 1:01 AM

Jan Schreuder said:

Yep, that is correct. So you can create a small batchfile to call this application.
# August 9, 2006 1:30 AM

Jan Schreuder on .Net said:

I've spent some time playing with Microsoft's solution to build documentation based on XML tags in your code. This post gives a brief recap of what I experienced as well as providing links to help you get started.
# August 11, 2006 11:51 PM

Syed Hasan Zubair said:

Thanks, Your aaplication help me alot.

# April 11, 2008 1:26 PM

Hitesh said:

Good work, I have used it and it has copied the DLLs from the GAC. Thanks

# September 23, 2008 11:49 PM

Shuvankar said:

Hugely helpful application. Thanks for this.

# May 27, 2009 9:31 AM

Srikanth said:

If you are looking for the physical location where your GACed DLL is saved in the file system, try this:

start-->run-->c:\windows\assembly\gac

If you don't find your DLL related folder in there, you can do a "Up" folder in windows explorer to display everything in c:\windows\assembly as folder structures. You can then look for your DLL under GAC_MSIL or any other folder out there....

Once, you identify your DLL, you can copy it as you normally do with windows explorer. Hope this helps!

Cheers,

Srikanth

# August 6, 2009 8:53 PM

Gaurav said:

Thanks, really helpful.

# September 4, 2009 7:10 AM

arshad said:

How i have installed both 2.0 and 3.5 . how i can copy crystal reports dlls for version 2.0

# December 31, 2009 7:23 AM

Ashish said:

You could use Xcopy command as well.

http://tinyurl.com/279zkvw

# December 28, 2010 6:13 AM

Naveed Mazhar said:

Hello,

I found the simple solution of copying a dll, at the following link:

www.tipscentre.net/.../Copy-DLL-From-GAC-Assembly.aspx

Hope this helps.

# January 13, 2011 11:49 AM

sanjeev said:

# February 15, 2012 8:00 PM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Please add 6 and 1 and type the answer here: