4

How can i get the location of the GAC directory using C#?

Exist an entry in the Windows Registry?

UPDATE

I need the location because, i want to enumerate and analyze the assemblies located in the GAC.

Bye.

jcollum
  • 43,623
  • 55
  • 191
  • 321
RRUZ
  • 134,889
  • 20
  • 356
  • 483
  • 2
    Woah, woah woah. Hold up there a minute! It's not *really* a folder. I think this is only a small part of what you're trying to accomplish; See: http://stackoverflow.com/questions/205188/whats-the-correct-way-of-registering-installing-an-assembly-to-the-gac – Robert P Oct 12 '09 at 22:28
  • 1
    There is no such thing as a "GAC Directory". The GAC is just a list of assemblies. – overstood Oct 12 '09 at 23:33
  • 3
    It's a virtual filesystem - seems overly pedantic to say it's not a directory. As you can see by doing a `dir %windir%\assembly`, it sure looks like a set of directories to the rest of the world! – Cheeso Oct 13 '09 at 00:26

5 Answers5

5

If you want to enumerate the stuff in the GAC (e.g. writing an system administration tool) your best option is to use the fusion.dll although this requires some interop code on your side.

Link to Microsoft for Fusion.DLL Documentation

Remko
  • 7,214
  • 2
  • 32
  • 52
Foxfire
  • 5,675
  • 21
  • 29
  • Trust me, you don't want to rely on where the GAC "appears" to be in the filesystem. It can change. – Joe Kuemerle Oct 13 '09 at 16:23
  • Exactly. Now that .NET 4.0 Beta 2 is public we see that MS has moved the location of the GAC to %SYSTEM%\Microsoft.NET\assembly\GAC* directories so we know that it is still best to use Fusion to find it. – Joe Kuemerle Oct 19 '09 at 16:24
2

There are absolutely valid reasons to access the GAC manually like a folder. Of course, one should be aware of the risk manipulating things there. But read-only access is quite ok, I would say.

Besides the mentioned %windir%\assembly, it might happen that Nir Sofer already did the job for you. He has written GACView.

And hey, it even has a feature to disable the special view in Explorer: Options/Disable Assembly Viewer in Explorer. The folder will then show up as a normal folder. While this worked well on Windows XP, it doesn't on Windows 7 64 Bit, because it's setting it in the 32 Bit version of the Registry. To disable the assembly viewer on Windows 7 64 Bit, start Regedit (64 Bit), go to

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion

and add a new DWORD called DisableCacheViewer with value 1.

To be independent from the directory (which might change), you can enumerate assemblies via the Fusion Registry Key:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Fusion\GACChangeNotification\Default

I guess this is how GACView works internally. But it still won't enumerate the .NET 4 assemblies in %windir%\Microsoft.NET\assembly.

A tool which supports .NET 4 is GACBrowser, but it displays less information.

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
1

Is it not always %windir%\assembly ?

edit:

c:\Windows>dir \windows\assembly
 Volume in drive C has no label.
 Volume Serial Number is C8BC-2EBD

 Directory of c:\windows\assembly

08/14/2009  03:06 AM    <DIR>          GAC
07/28/2009  03:07 AM    <DIR>          GAC_32
09/08/2009  10:57 PM    <DIR>          GAC_MSIL
08/15/2009  07:35 PM    <DIR>          NativeImages_v2.0.50727_32
09/08/2009  10:57 PM    <DIR>          temp
09/08/2009  09:47 PM    <DIR>          tmp
               0 File(s)              0 bytes
               6 Dir(s)   4,560,822,272 bytes free
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • No, it is not always going to be `%windir%\Assembly`. It happens to be that way right now, but it's current location and it's structure is not guaranteed to remain the same, nor is putting files in there a reasonable API for installing or reading information from the GAC. – Robert P Oct 12 '09 at 22:32
  • @Robert it is highly unlikely Microsoft will change it, since doing so would be a great risk to backward compatibility. (Not to say it's OK to try to do this) – Rex M Oct 12 '09 at 22:45
1

It is in the %windir%\Assembly folder.

Dan got the answer first as far as a code example though.

Alex Moore
  • 3,415
  • 1
  • 23
  • 39
  • 1
    are you going to keep it a secret then? – Cheeso Oct 12 '09 at 22:16
  • 1
    Probably because if you're looking for the GAC folder, you're doing something wrong. The "location" of the GAC is NOT fixed, and there is not an API for looking it up. – Robert P Oct 12 '09 at 22:29
  • I fully agree with you that there may be something (very) wrong if he needs that folder. On the other hand maybe he just wants do develop an info tool. So why not. In fact that would even make sense as the folder is not openable in Explorer by default. And the code sample shows that it is not hard to get the path. – Foxfire Oct 12 '09 at 22:35
  • Note that for .NET 4.0 Beta 2 the location of the GAC assemblies no longer defaults to %windir$\assembly. – Joe Kuemerle Oct 19 '09 at 16:25
1

Let the runtime manage its assemblies and trust that they'll be there. If you need to install something use gacutil. If you need to uninstall something use gacutil. If you need to access an assembly then add a reference to it in your project. If you need to interact with the GAC on someone else's machine, do what @Dan says and use MSI and friends.

Otherwise DO NOT TOUCH THE GAC

overstood
  • 985
  • 6
  • 7