views:

383

answers:

7

How can I tell from the assemblyName, or Assembly class (or others like it), whether an assembly is part of the .net framework (ie System.windows.Forms)?

So far I've considered the PublicKeyToken, and CodeBase properties, but these are not always the same for the whole framework.


Edit: The reason I want this info is to get a list of assemblies that my exe is using that need to be on client machines, so I can package the correct files in a setup file without using the Visual Studio setup system. The problem is, I don't want to pick up any Framework assemblies, and I want it to be an automatic process that is easy to roll out whenever a major update is finished.

The ultimate solution would be that there is an IsFramework property....:)

A: 

It begins with "System" ;)

Ben Scheirman
mscorlib doesn't as far as I know...
Dave Arkell
'mscorlib' contains mostly 'System.' namespaces though. But 'System' is not a reserved namespace element.
Anthony Mastrean
Neither do the Microsoft.* assemblies.
OwenP
Microsoft.* are not part of the Framework either.While System isn't reserved, all of the .NET framework assemblies do begin with System
Ben Scheirman
+3  A: 

It would be helpful if you can explain why you want to get this information. But I suspect that the method both most reliable and most general is going to be the PublicKeyToken. Yes, there's more than one, but it's going to be a finite list and one that doesn't change very often.

Edit: for that matter, you could just have a whitelist of assembly names -- that list, too, will be both finite and static between versions of the framework.

Curt Hagenlocher
I wonder if you can query some Microsoft service for the public key tokens... like the way they provide a remote symbol server. That way you don't have to track it manually.
Anthony Mastrean
+1  A: 

You could use reflection to look at the publisher of the assembly, and coordinate that with the assembly's path. If you find an assembly whose publisher is Microsoft, and which exists somewhere below c:\Windows\Microsoft.NET\Framework it's a safe bet it's part of the runtime.

Edit: On second though, the publisher may not even be necessary. Anything under that path should be part of the runtime (barring a misbehaving app that's diddling where it shouldn't be).

DannySmurf
The issue here is that a lot of stuff is in the GAC according to AssemblyName and Assembly
Dave Arkell
A: 

No it doesn't begin with "System", you could check "WindowsBase" which is a framework assembly.

You can't also check the PublicKeyToken because there are other Microsoft assemblies signed with the "default" keys but they are not part of the .NET Framework (VS assemblies).

The best way of doing it is to get a collection of installed .NET frameworks and check if the target assembly is part of their RedistList (RedistList\FrameworkList.xml)

Ion Todirel
+1  A: 

If you know that none of your DLLs will be in the GAC you could check whether each assembly is in the GAC or not. If it is, don't copy it. If it isn't, then do copy it. There is a property on the Assembly class called GlobalAssemblyCache. This would obviously work better in some situations than in others.

Jon Turner
+1  A: 

To accomplish this I'm using the Product Name embedded within the assembly through the AssemblyProductAttribute.

var attribute = assembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false)[0] as AssemblyProductAttribute;
var isFrameworkAssembly = (attribute.Product == "Microsoft® .NET Framework");

I'm using this technique to group assemblies by product under the About screen of the application and it seems to work just fine for me.

jpierson
+2  A: 

I have had to deal with the exact same issue. Unfortunately, all answers given so far are insufficient to safely determine if an assembly is part of the .NET Framework.

Microsoft puts a class named FXAssembly into the global namespace of each framework assembly with a const string indicating the version:

.class private abstract auto ansi sealed beforefieldinit FXAssembly
    extends [mscorlib]System.Object
{
    .field assembly static literal string Version = string('2.0.0.0')

}

Use this "marker" to check if an assembly is a framework assembly. Checking the public key too won't hurt either.

Johannes Rudolph