zoukankan      html  css  js  c++  java
  • UWP 调用Win32 关机

    话说最近程序需要个晚上自动关机的功能

    原则上 uwp 应该是没有关机权限的

    上网搜索之

    有人说只要这样就可以了

    var psi = new ProcessStartInfo("shutdown", "/s /t 0");
    psi.CreateNoWindow = true;
    psi.UseShellExecute = false;
    Process.Start(psi);

    但是使用这个必须要用 Brokered UWP Component Project Templates:

    https://marketplace.visualstudio.com/items?itemName=LanceContreras.BrokeredUWPComponentProjectTemplates

    这个 Templates 是 vs2015 的

    不过我的是 vs2017

    而且代码已经很多了,从头开始肯定不行

    但是在 VS2017 下,现在可以直接调用 Win32 程序

    在这里就可以调用 Win32 来进行关机

    首先随便写个关机的程序

    比如我们直接开个控制台程序,写上:

            static void Main(string[] args)
            {
                Process.Start("shutdown.exe", "-s -f -t 100");
            }

    设置程序开启后100秒关机 (话说设置100秒主要为了调试方便,只要运行 shutdown –a 就可以取消关机任务)

    编译后生成 ConsoleShutdown1.exe

    我们把文件拷贝到uwp的目录下面

    我单独建了个文件夹,把exe文件包含到项目中:

    image

    然后我们添加引用 “ Windows Desktop Extensions For The UWP ”,添加那个版本看自己的项目需要了,我项目的目标版本直接就是1709,所以直接引用最新版的扩展。

    (注意要在 UWP 中调用 Win32 程序,Windows Desktop Extensions For The UWP 的最低版本为 14393,也就是说对方的win10最低也要为1607)

    image

    然后我们需要编辑 Package.appxmanifest 文件

    直接查看代码:

    image

    在 Package 节点上,我们要加上 rescap 和 desktop 的引用,注意下面的 IgnorableNamespaces 要加上rescap ,不然你生成应用程序包的时候可以会提示配置文件错误。

    <Package
      xmlns="http://schemas.microsoft.com/appx/manifest/foundation/windows10"
      xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest"
      xmlns:uap="http://schemas.microsoft.com/appx/manifest/uap/windows10"
      xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
      xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
      IgnorableNamespaces="uap mp rescap">

    然后修改 Capabilities 节点,加上 <rescap:Capability Name="runFullTrust"/>

      <Capabilities>
        <Capability Name="internetClient" />
        <rescap:Capability Name="runFullTrust"/>
      </Capabilities>

    最后在 Application 节点中加入 Extensions 节点,里面包含我们的 Win32 程序在项目中的路径

      <Applications>
        <Application Id="App"
          Executable="$targetnametoken$.exe"
          EntryPoint="AppShutdown1.App">
          <uap:VisualElements
            DisplayName="AppShutdown1"
            Square150x150Logo="AssetsSquare150x150Logo.png"
            Square44x44Logo="AssetsSquare44x44Logo.png"
            Description="AppShutdown1"
            BackgroundColor="transparent">
            <uap:DefaultTile Wide310x150Logo="AssetsWide310x150Logo.png"/>
            <uap:SplashScreen Image="AssetsSplashScreen.png" />
          </uap:VisualElements>
          <Extensions>
            <desktop:Extension Category="windows.fullTrustProcess" Executable="ExeConsoleShutdown1.exe" />
          </Extensions>
        </Application>
      </Applications>

    基本就大功告成了

    下面就是在需要调用 Win32 程序的地方 写上:

    await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();

    就可以在UWP 中调用 Win32 程序了

    这里我就可以调用 shutdown 来关机

    而且即使在  “设置分配的访问权限” 下,也是可以正常调用 Win32程序 的

    image

    此外在调用 Win32 程序的时候还可以加参数(如果 Win32 程序支持的话)

    更多见: https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.fulltrustprocesslauncher

  • 相关阅读:
    Http Get Post put delete
    YII AR查询方法
    YII CJson类
    Yii 配置文件
    linux大文件分割 split命令
    chrome插件 postman插件 接口测试、API & HTTP 请求调试工具
    Yii 实现restful
    restful理解
    how tomcat works 读书笔记(一)----------一个简单的webserver
    HDU2206 IP的计算 【经典题】
  • 原文地址:https://www.cnblogs.com/sun8134/p/8429199.html
Copyright © 2011-2022 走看看