确定 C# 应用程序是否 运行 作为 Desktop Bridge 中的 UWP 应用程序(Project Centennial)

Determine if C# application is running as a UWP app in Desktop Bridge (Project Centennial)

如何检查应用程序是否 运行 作为 UWP?

,但我需要它用于 C#,而不是 C++。

如果我尝试获取 ApplicationData.Current.LocalFolder 并且应用程序是 运行 作为 Win32 应用程序,它会抛出异常:

An unhandled exception of type 'System.InvalidOperationException' occurred in applcation.exe Additional information: The process has no package identity. (Exception from HRESULT: 0x80073D54) occurred

如何无一例外地查看申请类型?

您看过这篇 MSDN 文章吗? Desktop Bridge – Identify the application’s context

它有一个示例,展示了如何根据 运行 模式(win32 桌面应用程序或桌面桥接应用程序)使应用程序表现不同。

您可以使用 P/Invoke 在 C# 中调用 GetCurrentPackageFullName 方法。有一个 DesktopBridge.Helpers 包可以用来为你做这件事。它也适用于 Windows 7.

DesktopBridge.Helpers helpers = new DesktopBridge.Helpers(); 
bool isUwp = helpers.IsRunningAsUwp(); 

有兴趣的可以看这里是如何实现的:https://github.com/qmatteoq/DesktopBridgeHelpers/blob/master/DesktopBridge.Helpers/Helpers.cs

这是 Nuget 包: https://www.nuget.org/packages/DesktopBridge.Helpers/