简体   繁体   中英

Get 'Application Display Name' in C# / UWP

According to the docs I can just use this: Windows.ApplicationModel.AppDisplayInfo.DisplayName

But I get error: An object reference is required for the non-static field, method, or property 'AppDisplayInfo.DisplayName

I can't add 'AppDiagnostics' capability or use the Package Display name which seems to be the most common solutions for this. Is the documentation wrong or am I doing something wrong?

If you just want the DisplayName of your current project, you can use the following code without the need to declare the appDiagnostics capability:

string displayName = Windows.ApplicationModel.AppInfo.Current.DisplayInfo.DisplayName;

If you want the DisplayName of all running apps including UWP apps, Win32 apps, system services and so on, you need to add the appDiagnostics capability in manifest first.

Check the steps to add the appDiagnostics capability:

  1. Add xmlns:rescap in Package and add rescap in IgnorableNamespaces . Then, add the appDiagnostics capability in Capabilities .
    <Package   xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" 
    IgnorableNamespaces="uap mp rescap">
      <Capabilities>
        <rescap:Capability Name="appDiagnostics" />
      </Capabilities>
    </Package>
  1. The appDiagnostics capability is a restricted capability, you need to get the permission from users. A user-consent prompt will be triggered at the first time calling the diagnostic APIs such as AppDiagnosticInfo.RequestInfoAsync() . Or users can open Settings panel, find the Privacy option and set the App Diagnostics .

在此处输入图片说明

Note that if permission is denied by users, then the APIs will only return information about the current app.

  1. Add the following code to view the DisplayName of all running apps.
var list = await AppDiagnosticInfo.RequestInfoAsync();
foreach (var info in list)
{
    string name = info.AppInfo.DisplayInfo.DisplayName;
}

如果有人需要它用于 WinUI 3

Windows.ApplicationModel.Package.Current.DisplayName

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