简体   繁体   中英

How to find out the ProductName property of another assembly?

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?

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);
    }

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.

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;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM