【问题标题】:UWP: how to start an exe file that is located in specific directory?UWP:如何启动位于特定目录中的 exe 文件?
【发布时间】:2018-08-17 18:21:48
【问题描述】:

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

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

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

到 Package.appmanifest 并调用它

await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();

在主类中。但是我需要将 app.exe 添加到项目的 Assets 目录中
我的问题是,如果 exe 文件位于其他目录中,如何在不完全添加 exe 文件的情况下启动它。
谢谢

【问题讨论】:

    标签: c# uwp windows-10-universal


    【解决方案1】:

    今天我编写了一个程序,可以成功地从 UWP 启动任何 .exe 程序。希望分享过程以造福他人。这是 stefan Wick MSFT 的答案的补充。首先需要更新 package.appmanifest。这就是我在 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>
    

    &lt;Extensions&gt; 标记中的代码负责启动可执行文件。带有&lt;Capabilities&gt; 标签的代码添加了启动可执行文件的能力或权限。像runFullTrust 这样的限制性能力在它下面有绿色的绿线。这不是错误,程序将在没有任何错误的情况下运行。上面代码中的Launcher.exe 是一个控制台应用程序。我在文本编辑器中编写代码并从中创建了 Launcher.exe。 Launcher.exe的代码是这样的:

    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 项目的 Assets 文件夹中。不允许 UWP 启动 .exe 应用程序。但 UWP 应用程序调用此代码来启动任何 .exe 程序。 GroupId ChromeGroup 用于通过将 chrome.exe 参数传递给 Launcher.exe 来启动 chrome 浏览器。 GroupId WordGroup 用于通过将 WINWORD.exe 参数传递给 Launcher.exe 来启动 MS Word。将参数传递给 Launcher.exe 的代码是:

    `private async void Button_Click(object sender, RoutedEventArgs e)
    {
    await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync("ChromeGroup");
    await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync("WordGroup");
    }`
    

    单击Api 上方的按钮将exe 文件的名称传递给Launcher.exe 程序。它通过接受GroupId 作为参数来做到这一点。 ApiWindows.ApplicationModel 命名空间下可用。

    编辑:

    您要启动的可执行文件可能未安装在系统上。它可能未与您的应用程序一起打包在 Assets 文件夹中。你可以在Parameters 属性中给出可执行文件的完整路径。

    【讨论】:

      【解决方案2】:

      您无法从 UWP 应用进程中启动任意 EXE。您可以做的是在您自己的包中启动您自己的完全信任的 EXE(正如您已经发现的那样)。然后,您可以从该 EXE 启动任意 EXE(假设用户有权访问它)。因此,在任意情况下,此场景的过程分为两步。

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

      【讨论】:

      • 嗨@Stefan,感谢您的回复。 From that EXE you can then launch the arbitrary EXE (assuming the user has the privileges to access it)。你能解释一下这方面的更多细节吗?我是 UWP 开发的新手。 A better option is to launch the other app via a protocol。这里的协议是什么?如果你能给我一个参考或这个选项的例子,对我来说会更好。谢谢
      • 你的包中完全信任的 EXE 可以只是一个哑启动器,根据从你的 UWP 传递的数据来启动你想要启动的实际 EXE。为此,您可以使用 ShellExecute、Process.Start、CreateProcess 或您希望在标准 Windows 桌面应用程序中使用的任何内容。我所说的协议启动是指 ms-word:// 或 mailto:// 之类的东西 - 但是这不适用于未实现此类协议的应用程序。
      • 感谢@Stefan,exe文件是从C项目构建的,因此它不支持协议激活。目前我可以通过在Package.appxmanifest中使用&lt;desktop:Extension Category="windows.fullTrustProcess" Executable="my.exe"/&gt;来执行exe,并且exe文件包含在appX包中。但是,是否可以添加多个&lt;desktop:Extension&gt; 来注册多个exe 文件。我试图这样做,但我得到了错误:The Extension element with Category attribute value "windows.fullTrustProcess" must only be declared once。你能帮我吗,谢谢。
      • @neo,您只需在清单中声明一个“fullTrustProcess”扩展,即您的启动器。根据从 UWP 传递的数据,此启动器可以启动任意数量的其他进程,无论是从包中的 EXE,还是从系统上其他位置的 EXE。
      • 否,在示例中,“/Sync”是启动时传递给 fulltrustprocess 的参数 LaunchFullTrustProcess("SyncGroup")。正如我之前所说,您只需要在清单中声明一个 fulltrustprocess,我们称之为 LAUNCHER.EXE。然后为您打算启动的每个附加 EXE 定义 ParameterGroups。 LAUNCHER.EXE 现在可以根据来自 UWP 的参数启动每个 EXE。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 2022-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      相关资源
      最近更新 更多