问题
What is the best way to list all available .NET 2.0 assemblies?
An example of the list needed is the one MS Visual Studio shows when you do 'Add Reference...' in the .NET tab.
I have read Visual studio uses its own directory configuration and the GAC another and .NET instalation another. Any ideas of how I can know where this directories are in a computer portable way (another computer might have windows installed in D: drive for example)?
From the information listed it must be possible to Assembly.Loadxxxx() it.
Note: It should be done programatically and not using gacutil for example (unless it provides a C# API). The objective of all this is to create a custom dynamic script editor so you understand the need to get to this information.
回答1:
First of all, there's an important difference between reference assemblies and assemblies in the GAC. To compile code, you need a reference assembly. To run code, you need either a copy of the assembly in the same folder as your .exe or the assembly in the GAC.
Normally, when you install a .NET application, its installer will copy the assemblies it uses in the GAC. Those assemblies are not usable as reference assemblies, you can't find out in what folder it is stored so you can't tell the compiler the proper value of its /reference command line argument. Well, you can find out but Microsoft tried to make it as hard as possible with a shell add-in.
Something different happens when you install a .NET application that allows you to use its assemblies in your own program. Like the .NET framework. It will make two copies of every assembly. One goes in the GAC, the other goes in a "well-known" location. For the .NET framework, these well-known locations are c:\windows\microsoft.net\ and c:\program files\reference assemblies. The latter folder started getting used by .NET 3.0 and up.
Visual Studio's Add Reference dialog uses a registry key that lists these well-known locations. There are a couple, but the important one is HKLM\Software\Microsoft\.NETFramework\AssemblyFolders
.
Long story short: you could use that registry key to produce the same list that the Add Reference dialog produces. But it is not 100% reliable, you might miss reference assemblies that some product copied somewhere else. You'd have to use the Browse tab in VS to add references to those. And search the entire disk to find them yourself.
回答2:
The only information I've found earlier about the same problem is this, but I never got around to use that information.
From the looks of it, it looks a bit cumbersome to traverse a directory to find all .DLL files, but perhaps that's what you need to do.
来源:https://stackoverflow.com/questions/495236/list-all-available-net-assemblies