简体   繁体   English

从命令行启动 Windows 10 UWP 应用程序 cmd

[英]Launching a Windows 10 UWP app from the command line cmd

I would like to be able to launch a Windows 10 UWP app from a single command line (cmd Windows 10) input.我希望能够从单个命令行 (cmd Windows 10) 输入启动 Windows 10 UWP 应用程序。 I am having issues concatenating the strings together to form the entire package name (this needs to be dynamic because the ending string of the app can change with each deployment).我在将字符串连接在一起以形成整个 package 名称时遇到问题(这需要是动态的,因为应用程序的结尾字符串可能会随着每次部署而改变)。 Here is the logic I have:这是我的逻辑:

  1. Find the app package name in the LocalAppData directory (ie packageName_postfix)在LocalAppData目录下找到app package名称(即packageName_postfix)
  2. Append "!App" to the end of the result in #1 Append "!App" 到 #1 中结果的末尾
  3. Pass that string into the launch function:将该字符串传递到启动 function 中:

I have gotten to a point where I can find #1, but for some reason when I try to append it to #2, I get a NULL value.我已经到了可以找到#1 的地步,但由于某种原因,当我尝试将 append 转换为#2 时,我得到了 NULL 值。 Here is my current command:这是我当前的命令:

1.) & 2.): Get Package Name and Append it to "!App" 1.) & 2.): 获取 Package Name 和 Append 到 "!App"

set "x=|dir %LocalAppData%\Packages /b | findstr packageName" & set "y=!App" & set "z=%x%%y%" & echo.%z%

3.): Launch UWP App 3.): 启动UWP App

explorer.exe shell:appsFolder\packageName_postfix!App

Any ideas?有任何想法吗?

First you must know this solution only work on Windows 10 Fall Creators Update and newer versions.首先,您必须知道此解决方案仅适用于Windows 10 Fall Creators Update和更新版本。 This API will support Windows Insider Build version 16266 or greater.此 API 将支持 Windows Insider Build 版本 16266 或更高版本。

Add this namespace in Pacakge.appxmanifest file :Pacakge.appxmanifest文件中添加这个命名空间:

xmlns:uap5="http://schemas.microsoft.com/appx/manifest/uap/windows10/5"  

Then add this extension :然后添加这个扩展:

<Extensions>  
    <uap5:Extension   
      Category="windows.appExecutionAlias"   
      Executable="MyApp.exe"   
      EntryPoint="MyApp.App">  
      <uap5:AppExecutionAlias>  
        <uap5:ExecutionAlias Alias="MyApp.exe" />  
      </uap5:AppExecutionAlias>  
    </uap5:Extension>  
  </Extensions>  
  • Executable name - Application.Exe name (Ex: MyApp.exe)可执行文件名称 - Application.Exe 名称(例如:MyApp.exe)
  • EntryPoint - Entrypoint tag should contain the Full name of the application class(Ref: open -> App.xaml file -> Application x: Class name) EntryPoint - Entrypoint 标记应包含应用程序类的全名(参考:打开 -> App.xaml 文件 -> 应用程序 x:类名)
  • AppExecutionAlias - Type alias name in command prompt to open the application; AppExecutionAlias - 在命令提示符中输入别名以打开应用程序; give the app.exe name as alias name.将 app.exe 名称作为别名。

Build the application and run it one time!构建应用程序并运行一次!

Open command Prompt, type the alias name -> Enter打开命令提示符,输入别名->回车

You can also see the reference Here您还可以在此处查看参考

SO with this step!所以这一步!

explorer.exe shell:appsFolder\**packageName_postfix**!**App**

how to get packageName_postfix and App?如何获取 packageName_postfix 和 App?

go to Win+R >> shell:appsFolder create shortcut for your app on desktop (right click) after try properties on shortcut and copy path of it.在尝试快捷方式的属性并复制它的路径后,转到 Win+R >> shell:appsFolder在桌面上为您的应用程序创建快捷方式(右键单击)。

finnaly win+r explorer.exe shell:appsFolder\\Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic最后 win+r explorer.exe shell:appsFolder\\Microsoft.ZuneMusic_8wekyb3d8bbwe!Microsoft.ZuneMusic

Adding some "pure command-line methods" using PowerShell. These can be assembled into a general-purpose script for finding the launch mechanism, but it's easier to explain the individual components below without "putting it all together" at once.使用 PowerShell 添加一些“纯命令行方法”。这些可以组装成用于查找启动机制的通用脚本,但无需立即“将它们放在一起”,更容易解释下面的各个组件。

First, note that you may not need the explorer.exe method any longer.首先,请注意您可能不再需要explorer.exe方法。 Many recent UWP apps now include an app execution alias for command-line access.许多最近的 UWP 应用程序现在包含用于命令行访问的应用程序执行别名。 Since this feature was introduced about 5 years ago now, it's available in all supported builds of Windows 10 and 11.由于此功能是大约 5 年前推出的,因此在 Windows 10 和 11 的所有支持版本中都可用。

The starting point for finding the launch command is always going to be the AppxPackage itself.查找启动命令的起点始终是AppxPackage本身。 But sometimes finding the package can be more difficult than others:但有时找到 package 可能比其他人更难:

  • The "easy" case -- Just filter Get-AppxPackage 's return results for one matching the Name . “简单”案例——只需过滤Get-AppxPackage的返回结果以找到与Name匹配的结果。 Using Windows Terminal as an example:以 Windows 终端为例:

     Get-AppxPackage |? { $_.Name -like "*Terminal*" } | tee-object -Variable packageMatches
  • However, some apps are oddly named.但是,某些应用程序的名称很奇怪。 For instance, the Minecraft Dungeons package name is Microsoft.Lovika , which I assume was its project name during development.例如,Minecraft Dungeons package 名称是Microsoft.Lovika ,我认为这是它在开发期间的项目名称。 Unless you know what you are looking for, you may want to perform a (slower) search based on the manifest DisplayName:除非您知道要查找的内容,否则您可能希望根据清单 DisplayName 执行(较慢的)搜索:

     Get-AppxPackage |? { (Get-AppxPackageManifest -Package $_.PackageFullName).Package.Properties.DisplayName -like "*Mine*" } | tee -variable packageMatches

Once you have the $packageMatches result, you can use one of two different ways to find how to launch it:获得$packageMatches结果后,您可以使用两种不同的方法之一来查找如何启动它:

  • Using the fully-qualified path to the app execution alias, if one is available.使用应用程序执行别名的完全限定路径(如果有的话)。 To determine this:要确定这一点:

     $packageMatches |% { $executableName = ((Get-AppxPackageManifest -Package $_.PackageFullName).Package.Applications.Application.Executable) if ($executableName) { "Start-Process ""$($_.InstallLocation)\$executableName""" } else { "No executable found" } }

    Note: The Start-Process format that this returns is simply for launching from PowerShell with simplified quoting rules for paths-with-spaces.注意:此返回的Start-Process格式仅用于从 PowerShell 启动,并使用带空格的路径的简化引用规则。 Adapt as needed for other launchers.根据需要适应其他发射器。

  • If there is no app execution alias for the app, you can fall back to using the explorer.exe shell:appsFolder\<PackageFamilyName>!<Id> method covered in other answers.如果应用程序没有应用程序执行别名,您可以回退到使用其他答案中介绍的explorer.exe shell:appsFolder\<PackageFamilyName>!<Id>方法。 To obtain the correct name and id:要获取正确的名称和 ID:

     $packageMatches |% { "explorer.exe shell:appsFolder\$($_.PackageFamilyName).$((Get-AppxPackageManifest -Package $_.PackageFullName).Package.Applications.Application.Id)" }

    For example, any PWA (progressive web app) or website installed through Edge as an application will be not have an executable, but will be launchable through explorer.exe .例如,通过 Edge 作为应用程序安装的任何 PWA(渐进式 web 应用程序)或网站将没有可执行文件,但可以通过explorer.exe启动。

It's possible creating a shortcut (.lnk file) and then shell execute the shortcut, this might not be perfect solution but at least should be very fast, using PowerShell for instance needs at least 250 ms for startup, and also shows a console window, which might be unwanted.可以创建快捷方式(.lnk 文件),然后 shell 执行快捷方式,这可能不是完美的解决方案,但至少应该非常快,例如使用 PowerShell 至少需要 250 毫秒才能启动,并且还显示控制台 window,这可能是不需要的。 There is a tool that can hide the console window: run-hidden .有一个工具可以隐藏控制台 window: run-hidden

It's also a very simple solution anybody can use, does not require authoring and learning a complex script or app, simple solutions are sometimes overlooked.这也是一个任何人都可以使用的非常简单的解决方案,不需要编写和学习复杂的脚本或应用程序,简单的解决方案有时会被忽视。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 CMD 上 Windows 从文本行回显变量值 - CMD on Windows echo variable value from text line C#启动带有转义字符的命令行 - C# launching command line with escape characters 如何在txt文件中找到数字序列并用Windows 10的CMD保存? - How to find the sequence of numbers in a txt file and save it with CMD of Windows 10? 字符串操作命令行窗口7 - string manipulation command line windows 7 如何在cmd命令行中设置和使用文件名字符串变量? - How to set and use filename string variables in cmd command line? 如何获取Windows命令行字符串? - how to get windows command line string? 如何在 Windows 命令行上删除字符串中的前缀? - How to remove prefixes in strings on Windows command line? Windows命令行上的引号周围的引号 - Quotes around quotes on the Windows command line 我正在使用 python 中的 powershell 命令获取一些数据,而在 Windows 中没有 cmd! 如何将此输出分配给字符串变量? - I am getting some data using powershell command from python without having a cmd in windows! how can i assigned this output to a string variable? 在线编译器(在线 gdb)工作正常,而 g++ 和 gcc on windows 10 cmd 在比较字符串时不起作用 - online compiler(online gdb)works correctly while g++ and gcc on windows 10 cmd doesn't work when comparing strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM