C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – UWP:如何启动位于特定目录中的exe文件?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试从UWP应用程序启动位于C:/ Program Files(x86)/ App中的exe.我怎样才能做到这一点.

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

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

到Package.appmanifest并调用

await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();

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

解决方法

今天我写了一个程序来成功启动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>

< Extensions>中的代码tag是负责启动可执行文件标签.包含<功能>的代码标签添加启动可执行文件功能或权限.像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作为参数来完成此操作. Api在Windows.ApplicationModel NameSpace下可用.

编辑:

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

大佬总结

以上是大佬教程为你收集整理的c# – UWP:如何启动位于特定目录中的exe文件?全部内容,希望文章能够帮你解决c# – UWP:如何启动位于特定目录中的exe文件?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: