Home > Blockchain >  Getting application GUID in a .NET Core 6 application
Getting application GUID in a .NET Core 6 application

Time:02-02

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?

CodePudding user response:

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; 
          

CodePudding user response:

In .netFramework,you could find AssemblyInfo.cs directly:

enter image description here

And modify the settings: enter image description here

In, .net core,Select Show All Files and search AssemblyInfo.cs

enter image description here

In my case, the attributes you want haven't been attached to the Assembly by default(the two attribute was added by me mannully,not generated automaticlly):

enter image description here

You would access the properties now after adding the two line:

enter image description here

However,We shouldn't modify this file manually.

Then I searched and found this enter image description here

When it comes to company,you still could set here,or in .csproj file enter image description here

enter image description here

  • Related