Question

I want to get Assemblies Friendly Names in Current Application Domain and hence I wrote something like this :

static void Main(string[] args)
    {
        foreach (System.Reflection.Assembly item in AppDomain.CurrentDomain.GetAssemblies())
        {
          Console.WriteLine(item.FullName);

        }
    }

But the problem is this is the output what I got rather than what I desired to see :

mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e0 ApplicationDomains, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Actually I was expecting those names :

alt text http://www.pixelshack.us/images/xjfkrjgwqiag9s6o76x6.png

Can someone tell me if there is something I mistook.

Thanks in advance.

Or the names I was expecting weren't assemblies ?

Was it helpful?

Solution

You won't always get the pretty namespace names when you use reflection, you get the real names of the assemblies.

You also won't get all referenced libraries, only the ones that CURRENTLY loaded. If you add "XmlDocument foo = new XmlDocument()" above your code, System.XML will show up.

    static void Main(string[] args)
    {
        XmlDocument foo = new XmlDocument();

        foreach (System.Reflection.Assembly item in AppDomain.CurrentDomain.GetAssemblies())
        {
            Console.WriteLine(item.FullName);

        }
    }

Output:

mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
ConsoleApplication2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089

OTHER TIPS

It's impossible to get list of all referenced assemblies during runtime. Even if you reference it in your visual studio project, if you don't use them, C# compiler will ignore them and therefore they won't make it into your output file (exe/dll) at all.

And for the rest of your assemblies, they won't get loaded until they are actually used.

AppDomain.CurrentDomain.GetAssemblies() gives you array of all loaded assemblies and this list could be very different from what you see in visual studio project.

    foreach(var assem in AppDomain.CurrentDomain.GetAssemblies())
    {
        Console.WriteLine(assem.GetName().Name);

    }

Assembly.GetName() returns an AssemblyName object which has a Name property. That's what you're looking for.

Either use Assemly.GetName().Name or use reflection to find the AssemblyTitleAttribute and use that value.

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