Question

How can I tell from the assembly name, or assembly class (or others like it), whether an assembly is part of the .NET framework (that is, System.windows.Forms)?

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

The reason I want this information is to get a list of assemblies that my EXE file 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 .NET 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... :)

Was it helpful?

Solution

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.

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.

OTHER TIPS

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.

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.

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 (Visual Studio 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).

FrameworkList.xml can be found in:

  • .NET 2.0: C:\Windows\Microsoft.NET\Framework64\v2.0.50727\RedistList
  • .NET 3.x: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\vVersionNumber\RedistList
  • .NET 4.x: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\vVersionNumber\RedistList
  • .NET Core: C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETCore\vVersionNumber\RedistList

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.

When you install Visual Studio, you get the reference assemblies in various subfolders of the form C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\{FrameworkName}\{FrameworkVersion} - the most interesting thing could be the RedistList\FrameworkList.xml file that contains a list of all assembly names that were shipped with the given framework version.

E.g. C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\RedistList\FrameworkList.xml seems to contain a list of all of .NET 4.0's Framework assemblies.

You could easily use these files to establish static white lists of assemblies.

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top