Files
LanMountainDesktop/platform/LanMountainDesktop.Platform/WindowsPackageIdentity.cs
2026-08-11 19:41:54 +08:00

44 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Runtime.InteropServices;
using System.Text;
namespace LanMountainDesktop.Platform.Windows;
/// <summary>
/// Windows 包标识查询MSIX/UWP 打包检测)。
/// </summary>
public static class WindowsPackageIdentity
{
private const int AppmodelErrorNoPackage = 15700;
/// <summary>
/// 检测当前进程是否具有包标识(以 MSIX 打包运行)。
/// 非 Windows 平台返回 false。
/// </summary>
public static bool HasPackageIdentity()
{
if (!OperatingSystem.IsWindows())
{
return false;
}
var length = 0;
var hr = GetCurrentPackageFullName(ref length, null);
if (hr == AppmodelErrorNoPackage)
{
return false;
}
if (length <= 0)
{
return hr == 0;
}
var builder = new StringBuilder(length);
hr = GetCurrentPackageFullName(ref length, builder);
return hr == 0;
}
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
private static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder? packageFullName);
}