0

when I programmed in .NET Framework, I used to get the GUID of the Winforms application this way:

static public string AssemblyGuid
    {
        get
        {
            object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);
            if (attributes.Length == 0)
            {
                return String.Empty;
            }
            return ((System.Runtime.InteropServices.GuidAttribute)attributes[0]).Value;
        }
    }

And I could even get other values, such as the company, this way:

static public string AssemblyCompany
    {
        get
        {
            object[] attributes = Assembly.GetEntryAssembly().GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
            if (attributes.Length == 0)
            {
                return "";
            }
            return ((AssemblyCompanyAttribute)attributes[0]).Company;
        }
    }

That in .NET Core does not work, for example, an empty GUID is returned.

How can I do it?

jstuardo
  • 3,901
  • 14
  • 61
  • 136

1 Answers1

0

I just created a Winform project using Net 7 and ran your code it didn't return GuidAttribute your code is looking for. The solution is to add an AssemblyInfo to your project for example:

using System.Runtime.InteropServices;

// In SDK-style projects such as this one, several assembly attributes that were historically
// defined in this file are now automatically added during build and populated with
// values defined in project properties. For details of which attributes are included
// and how to customise this process see: https://aka.ms/assembly-info-properties


// Setting ComVisible to false makes the types in this assembly not visible to COM
// components.  If you need to access a type in this assembly from COM, set the ComVisible
// attribute to true on that type.

[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM.

[assembly: Guid("d3870e08-eaf6-4d62-b192-bb2cb42f2fe6")]

The following code will return you the executing assembly id

object[] attributes = Assembly.GetExecutingAssembly()
.GetCustomAttributes(typeof(System.Runtime.InteropServices.GuidAttribute), false);

var assemblyId = ((System.Runtime.InteropServices.GuidAttribute)attributes[0]).Value; 
          
Muhammad Hannan
  • 2,389
  • 19
  • 28
  • I should add that file manually? in what project location? That is the only content it will have and I need to generate a new GUID using Create GUID option in the tools menu or do I have to get it from somewhere else? – jstuardo Feb 02 '23 at 22:30
  • Right click on your project > Add > New Item > AssemblyInfo.cs. That's it. – Muhammad Hannan Feb 03 '23 at 14:33