UWP:如何启动位于特定目录中的 exe 文件?
UWP: how to start an exe file that is located in specific directory?
我正在尝试从 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();
主要 class。但是我需要将 app.exe 添加到项目
的 Assets 目录中
我的问题是,如果 exe 文件位于其他目录中,我如何在不准确添加 exe 文件的情况下启动它。
谢谢
您不能从您的 UWP 应用进程启动任意 EXE。你可以做的是在你自己的包中启动你自己的完全信任的 EXE(正如你已经发现的那样)。然后您可以从该 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>
标签内的代码负责启动可执行文件。带有 <Capabilities>
标签的代码添加了启动 executable.The 限制性功能(如 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 项目的资产文件夹中。不允许 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
命名空间下可用。
编辑:
您要启动的可执行文件可能没有安装在系统上。它可能不会与您的应用程序一起打包在资产文件夹中。比你可以在 Parameters
属性中给出可执行文件的完整路径。
我正在尝试从 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();
主要 class。但是我需要将 app.exe 添加到项目
的 Assets 目录中
我的问题是,如果 exe 文件位于其他目录中,我如何在不准确添加 exe 文件的情况下启动它。
谢谢
您不能从您的 UWP 应用进程启动任意 EXE。你可以做的是在你自己的包中启动你自己的完全信任的 EXE(正如你已经发现的那样)。然后您可以从该 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>
标签内的代码负责启动可执行文件。带有 <Capabilities>
标签的代码添加了启动 executable.The 限制性功能(如 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 项目的资产文件夹中。不允许 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
命名空间下可用。
编辑:
您要启动的可执行文件可能没有安装在系统上。它可能不会与您的应用程序一起打包在资产文件夹中。比你可以在 Parameters
属性中给出可执行文件的完整路径。