views:

52

answers:

3

I have two assemblies A.exe and B.exe. Both are Windows.Forms .net 3.5 assemblies. A.exe knows that B.exe is in the same directory.

How can I find out the ProductName of B.exe from A.exe?

A: 

The following is an example on how to read assembly information through code.

http://www.c-sharpcorner.com/UploadFile/ravesoft/Page112282007015536AM/Page1.aspx

And you can load a specific assembly using [Assembly.Load()][1] method.

decyclone
Yes I'm aware of that. But how do I load B.exe? I only know its name, not its 'long form name' or 'assemblyname' as required by Assembly.Load().
Marc
`Assembly.LoadFile(System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "B.exe"))`
decyclone
A: 

This method might help you.

You need the name space "System.Reflection" to use the code below.


    //fileName = @"...\B.exe"; //The full path of the file you want to load

    public string GetAssemblyProductName(string fileName)
    {
        Assembly fileAssembly = null;

        try
        {
            fileAssembly = Assembly.LoadFile(fileName);//Loading Assembly from a file
        }
        catch (Exception error)
        {
            Console.WriteLine("Error: {0}", error.Message);
            return string.Empty;
        }

        if (fileAssembly != null)
        {
            string productName = fileAssembly.GetName().Name;//This is for getting Product Name
            //string productName = fileAssembly.GetName().FullName;//This is for getting Full Name
            return productName;
        }
        else 
        {
            Console.WriteLine("Error: Not valid assembly.");
            return string.Empty;
        }
    }
Mario
+3  A: 

The FileVersionInfo class is useful here. The [AssemblyProduct] attribute gets compiled into the unmanaged version info resource. This code works on any .exe:

    private void button1_Click(object sender, EventArgs e) {
        var info = System.Diagnostics.FileVersionInfo.GetVersionInfo(@"c:\windows\notepad.exe");
        MessageBox.Show(info.ProductName);
    }
Hans Passant
Easy and simple, works perfectly, thanks a lot! [+1]
Marc