首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >UWP:如何启动位于特定目录中的exe文件?

UWP:如何启动位于特定目录中的exe文件?
EN

Stack Overflow用户
提问于 2018-03-09 08:18:00
回答 2查看 7K关注 0票数 11

我正在尝试启动一个位于C:/Program (x86)/App的UWP应用程序中的exe。我该怎么做呢。

我可以使用UWP的Windows扩展名启动exe文件,添加隐藏复制代码

代码语言:javascript
运行
复制
<Extensions>
        <desktop:Extension Category="windows.fullTrustProcess"          Executable="Assets\app.exe" />
</Extensions>

到Package.appmanifest并将其调用

代码语言:javascript
运行
复制
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();

在主修课上。但是我需要将app.exe添加到项目的资产目录中。

我的问题是,如果exe文件位于其他目录中,我如何才能启动它而不添加确切的exe文件。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-03-17 19:06:11

今天,我编写了一个程序来成功地启动来自UWP的任何.exe程序。为了其他人的利益,想要分享这个过程。这是除了stefan Wick 的答案。首先,需要更新package.appmanifest。这就是我在package.appmanifest中拥有的内容:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>

<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:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"

  IgnorableNamespaces="uap mp">

  <Identity
    Name="217d09c4-aa67-4403-939f-518a55d46f16"
    Publisher="CN=admin"
    Version="1.0.0.0" />

  <mp:PhoneIdentity PhoneProductId="217d09c4-aa67-4403-939f-518a55d46f16" PhonePublisherId="00000000-0000-0000-0000-000000000000"/>

  <Properties>
    <DisplayName>App1</DisplayName>
    <PublisherDisplayName>admin</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>

  <Dependencies>
    <TargetDeviceFamily Name="Windows.Universal" MinVersion="10.0.14393.0" MaxVersionTested="10.0.16299.0" />
  </Dependencies>

  <Resources>
    <Resource Language="x-generate"/>
  </Resources>

  <Applications>
    <Application Id="App"
      Executable="$targetnametoken$.exe"
      EntryPoint="App1.App">
      <uap:VisualElements
        DisplayName="App1"
        Square150x150Logo="Assets\Square150x150Logo.png"
        Square44x44Logo="Assets\Square44x44Logo.png"
        Description="App1"
        BackgroundColor="transparent">
        <uap:DefaultTile Wide310x150Logo="Assets\Wide310x150Logo.png"/>
        <uap:SplashScreen Image="Assets\SplashScreen.png" />
      </uap:VisualElements>

        <Extensions>

          <desktop:Extension Category="windows.fullTrustProcess" Executable="Assets\Launcher.exe" >
          <desktop:FullTrustProcess>
            <desktop:ParameterGroup GroupId="ChromeGroup" Parameters="chrome.exe"/>
            <desktop:ParameterGroup GroupId="WordGroup" Parameters="WINWORD.exe"/>
          </desktop:FullTrustProcess>
          </desktop:Extension>
        </Extensions>

    </Application>
  </Applications>

  <Capabilities>

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

  </Capabilities>

</Package>

<Extensions>标记中的代码负责启动可执行文件。带有<Capabilities>标记的代码--添加功能或启动executable.The限制性功能的权限,比如runFullTrust --在代码下面有绿线。这不是一个错误,程序将运行没有任何错误。以上代码中的Launcher.exe是一个控制台应用程序。我在文本编辑器中编写代码并从中创建Launcher.exe。Launcher.exe的代码如下:

代码语言:javascript
运行
复制
using System;  
using System.IO;   
using System.Diagnostics;                                                                         
using System.Reflection;
    class Program
    {
    static void Main(string []args)
    {
    try
    {

    if(args.Length!=0)
    {
    string executable=args[2];
   /*uncomment the below three lines if the exe file is in the Assets  
    folder of the project and not installed with the system*/         
    /*string path=Assembly.GetExecutingAssembly().CodeBase;
    string directory=Path.GetDirectoryName(path);
    process.Start(directory+"\\"+executable);*/
    Process.Start(executable);
    }
    }
    catch(Exception e)
    {
    Console.WriteLine(e.Message);
    Console.ReadLine();
    }

    }
    }

将此Launcher.exe控制台应用程序保存在UWP项目的资产文件夹中。UWP不允许发布.exe应用程序。但是UWP应用程序调用这个代码来启动任何.exe程序。GroupId ChromeGroup用于通过向Launcher.exe传递chrome.exe参数来启动chrome浏览器。GroupId WordGroup用于通过将WINWORD.exe参数传递给Launcher.exe来启动MS。将参数传递给Launcher.exe的代码是:

代码语言:javascript
运行
复制
`private async void Button_Click(object sender, RoutedEventArgs e)
{
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync("ChromeGroup");
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync("WordGroup");
}`

单击Api上面的一个按钮,将exe文件的名称传递给Launcher.exe程序。它通过接受GroupId作为参数来实现这一点。Api可以在Windows.ApplicationModel NameSpace下使用。

编辑:

要启动的可执行文件可能不会安装在系统上。它可能不会与应用程序一起打包在“资产”文件夹中。无法在Parameters属性中给出可执行文件的完整路径。

票数 17
EN

Stack Overflow用户

发布于 2018-03-09 21:38:52

您不能从您的UWP应用程序进程启动任意的EXE。您可以做的是在您自己的包中启动您自己的完全信任EXE (正如您已经发现的那样)。然后,您可以从该EXE启动任意EXE (假设用户拥有访问它的权限)。因此,在任意情况下,这是一个两个步骤的过程。

一个更好的选择是通过协议启动另一个应用程序(但如果您不拥有它,或者它不支持协议激活,这可能并不总是一个选项)。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49189353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档