UWP:如何启动位于特定目录中的exe文件? [英] UWP: how to start an exe file that is located in specific directory?

查看:93
本文介绍了UWP:如何启动位于特定目录中的exe文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to start an exe that is located in C:/Program Files (x86)/App from UWP app. How can I do this.

我可以使用Windows Desktop Extension for UWP启动exe文件,添加
隐藏复制代码

I can start exe file by using Windows Desktop extension for UWP, add Hide Copy Code

<Extensions>
        <desktop:Extension Category="windows.fullTrustProcess"          Executable="Assets\app.exe" />
</Extensions>

到Package.appmanifest并将其称为

to Package.appmanifest and call this

await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();

在主类中。但是我需要将app.exe添加到项目

的Assets目录中。我的问题是exe文件是否位于其他目录中,如何在不完全添加exe文件的情况下启动它。 b谢谢

in main class. But I need to add app.exe to Assets directory of project
My question is if exe file is located in other directory, how can I start it without adding exactly the exe file.
Thanks

推荐答案

今天,我编写了一个程序,可以从UWP成功启动任何.exe程序。希望共享过程以造福他人。这是 stefan Wick MSFT 回答的补充。首先,package.appmanifest需要更新。这是我在package.appmanifest中所拥有的:

Today I wrote a program to successfully launch any .exe programs from UWP. Want to share the process for the benefit of others. This is in addition to the answer by stefan Wick MSFT. First the package.appmanifest needs to be updated. This is what I have in package.appmanifest:

<?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> 标签的代码添加了启动可执行文件的功能或许可。 runFullTrust 等限制性功能具有它下面的绿色绿色线。这不是错误,程序将正常运行。上面的代码中的 Launcher.exe 是一个控制台应用程序。我在文本编辑器中编写代码,并从中创建Launcher.exe。 Launcher.exe的代码如下:

The code within the <Extensions> tag is the one responsible for launching the executable files. The code with <Capabilities> tag add capability or permission to launch the executable.The restrictive capabilities like runFullTrust is having green green line below it. It is not a error and program will run without any error. The Launcher.exe in the above code is a console app. I write the code in the text editor and created Launcher.exe from it. The code of the Launcher.exe is this:

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();
    }

    }
    }

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

Saved this Launcher.exe console app in Assets folder of the UWP project. UWP is not allowed to launch .exe apps. But UWP app calls this code to launch any .exe program. The GroupId ChromeGroup is used to launch chrome browser by passing chrome.exe parameter to the Launcher.exe. The GroupId WordGroup is used to launch MS Word by passing WINWORD.exe parameter to Launcher.exe. The code to pass the parameter to Launcher.exe is :

`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 命名空间下可用。

On the click of a button above Api pass on the name of the exe file to Launcher.exe program. It do this by accepting GroupId as parameter. The Api is available under Windows.ApplicationModel NameSpace.

编辑:

您要启动的可执行文件可能未安装在系统。它可能不会与您的应用程序打包在Assets文件夹中。比您可以在 Parameters 属性中提供可执行文件的完整路径。

The executable you want to launch may not be installed on system. It may not be packaged with your application in Assets folder. Than you can give full path of the executable in the Parameters attribute.

这篇关于UWP:如何启动位于特定目录中的exe文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆