代码之家  ›  专栏  ›  技术社区  ›  Marc

如何找到另一个程序集的ProductName属性?

  •  0
  • Marc  · 技术社区  · 16 年前

    我有两个程序集A.exe和B.exe。两者都是Windows.Forms.net 3.5程序集。A.exe知道B.exe在同一目录中。

    我怎样才能找到答案 从A.exe中删除B.exe?

    3 回复  |  直到 16 年前
        1
  •  9
  •   Hans Passant    16 年前

    FileVersionInfo类在这里很有用。[AssemblyProduct]属性被编译到非托管版本信息资源中。此代码适用于任何.exe:

        private void button1_Click(object sender, EventArgs e) {
            var info = System.Diagnostics.FileVersionInfo.GetVersionInfo(@"c:\windows\notepad.exe");
            MessageBox.Show(info.ProductName);
        }
    
        2
  •  0
  •   decyclone    16 年前

    下面是一个如何通过代码读取程序集信息的示例。

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

    您可以使用 [Assembly.Load()][1] 方法。

        3
  •  -1
  •   Mario    16 年前

    这个方法可能对你有帮助。


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