zoukankan      html  css  js  c++  java
  • 通过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相关参数。

  • 相关阅读:
    聚集索引和非聚集索引的区别有哪些
    材料管理框架:一个共通的viewModel搞定所有的分页查询
    常用到的Linux命令
    Mybatis的使用中的一些不太注意的技巧
    Maven使用yuicompressor-maven-plugin打包压缩css、js文件
    Redis实现Mybatis的二级缓存
    zookeeper分布式协调服务的使用一
    Redis Cluster集群
    Spring+Struts2+Hibernate的整合
    SpringMVC,采用的是SpringJDBC
  • 原文地址:https://www.cnblogs.com/duwenlong/p/15682853.html
Copyright © 2011-2022 走看看