通过NetworkIsolationEnumAppContainers查看安装的UWP应用
很多时候有启动UWP的程序需求,启动之前检查一下当前程序是否存在。
比如我们查看显示名称、工作路径、包名等等。都可以通过NetworkIsolationEnumAppContainers方法获取,这个代码是在通过NetworkIsolationEnumAppContainers 检索到的github上的一个代码,减去一些不需要的部分后,剩余的代码:
LoopUtil.cs
public class LoopUtil
{
internal List<LoopUtil.INET_FIREWALL_APP_CONTAINER> _AppList;
internal IntPtr _pACs;
public LoopUtil()
{
LoadApps();
}
private List<INET_FIREWALL_APP_CONTAINER> PI_NetworkIsolationEnumAppContainers()
{
IntPtr arrayValue = IntPtr.Zero;
uint size = 0;
var list = new List<INET_FIREWALL_APP_CONTAINER>();
GCHandle handle_pdwCntPublicACs = GCHandle.Alloc(size, GCHandleType.Pinned);
GCHandle handle_ppACs = GCHandle.Alloc(arrayValue, GCHandleType.Pinned);
uint retval = NetworkIsolationEnumAppContainers((Int32)NETISO_FLAG.NETISO_FLAG_MAX, out size, out arrayValue);
_pACs = arrayValue; //store the pointer so it can be freed when we close the form
var structSize = Marshal.SizeOf(typeof(INET_FIREWALL_APP_CONTAINER));
for (var i = 0; i < size; i++)
{
var cur = (INET_FIREWALL_APP_CONTAINER)Marshal.PtrToStructure(arrayValue, typeof(INET_FIREWALL_APP_CONTAINER));
list.Add(cur);
arrayValue = new IntPtr((long)(arrayValue) + (long)(structSize));
}
//release pinned variables.
handle_pdwCntPublicACs.Free();
handle_ppACs.Free();
return list;
}
public void LoadApps()
{
_AppList = PI_NetworkIsolationEnumAppContainers();
}
[StructLayoutAttribute(LayoutKind.Sequential)]
internal struct INET_FIREWALL_APP_CONTAINER
{
internal IntPtr appContainerSid;
internal IntPtr userSid;
[MarshalAs(UnmanagedType.LPWStr)]
public string appContainerName;
[MarshalAs(UnmanagedType.LPWStr)]
public string displayName;
[MarshalAs(UnmanagedType.LPWStr)]
public string description;
internal INET_FIREWALL_AC_CAPABILITIES capabilities;
internal INET_FIREWALL_AC_BINARIES binaries;
[MarshalAs(UnmanagedType.LPWStr)]
public string workingDirectory;
[MarshalAs(UnmanagedType.LPWStr)]
public string packageFullName;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
internal struct INET_FIREWALL_AC_CAPABILITIES
{
public uint count;
public IntPtr capabilities;
}
[StructLayoutAttribute(LayoutKind.Sequential)]
internal struct INET_FIREWALL_AC_BINARIES
{
public uint count;
public IntPtr binaries;
}
[DllImport("FirewallAPI.dll")]
internal static extern uint NetworkIsolationEnumAppContainers(uint Flags, out uint pdwCntPublicACs, out IntPtr ppACs);
enum NETISO_FLAG
{
NETISO_FLAG_FORCE_COMPUTE_BINARIES = 0x1,
NETISO_FLAG_MAX = 0x2
}
}
MainWindow.xaml的构造函数中添加引用
LoopUtil loopUtil = new LoopUtil();
loopUtil.LoadApps();
var uwpTempApplist = loopUtil._AppList;
uwpTemoApplist中就是存放的UWP相关参数。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?