首页 > 解决方案 > 从当前应用程序打开另一个应用程序

问题描述

我们在 Windows 应用商店中有一个 UWP 应用。通过这个应用程序,我们想在同一系统上启动各种应用程序。对于这个过程,我们需要做两件事。

  1. 检查系统上是否存在应用程序
  2. 如果是,请启动它。如果没有,请反馈

我们尝试了几件事,但我正在寻找最好的方法来做到这一点。我们希望同时启动其他 UWP 应用和独立应用。

我尝试弄乱 Unity PlayerPrefs,但这很奇怪。如果我制作自定义 PlayerPref 并检查它是否存在于 1 个应用程序中,它会起作用,但是一旦我在 UWP 中制作了 playerpref 并在 Standalone 中检查它,我什么也得不到。当然反之亦然。(是的,我知道 UWP 将其 playerprefs 保存在其他地方)

什么是最好的通用解决方案?继续弄乱 Playerprefs 并根据我们要打开的应用程序搜索不同的路径?(独立,UWP)或其他方式?

编辑:到目前为止我所拥有的:

        if (Input.GetKeyDown(KeyCode.Backspace))
    {
        PlayerPrefs.SetString("42069" , "testing_this");
        PlayerPrefs.Save();
        Debug.Log("Wrote key 42069 to registry with: -value testing_this-");
    }

    if (Input.GetKeyDown(KeyCode.Space))
    {
        if (PlayerPrefs.HasKey("42069"))
        {
            Debug.Log("I found the key 42069 in my registry");
            cube.SetActive(true);
        }
        else
        {
            Debug.Log("I cant find key 42069 in my registry");
        }
    }

    if (Input.GetKeyDown(KeyCode.S))
    {
        const string registry_key = @"SOFTWARE\DefaultCompany";
        using(RegistryKey key = Registry.CurrentUser.OpenSubKey(registry_key))
        {
            if (key != null)
                foreach (string subKeyName in key.GetSubKeyNames())
                {
                    if (subKeyName == "RegistryTesting")
                    {
                        Debug.Log("I found the key on path: " + registry_key);
                    }
                }
        }
    }

编辑:没有人?我知道有办法。我需要做的就是检查 UWP 应用程序中是否存在独立应用程序。但我无法访问 UWP 应用程序中的寄存器。我知道有一些方法可以使用桥梁等,但我不知道如何以及从哪里开始。

标签: c#unity3duwp

解决方案


我遇到了类似的情况,但我正在检查应用程序是否正在运行,如果没有,请启动它。在我的情况下,我想要检查和启动的应用程序不是我编写的,也不是 UWP,因此我的解决方案可能不适合您,因为这样做的功能受到限制。

首先将受限功能添加到 package.appxmanifest(代码)。

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap"

然后向应用程序添加“appDiagnostics”功能。

<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="appDiagnostics" />
</Capabilities>

现在您可以请求访问正在运行的进程并进行检查的权限。

using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.System;
using Windows.System.Diagnostics;
class ProcessChecker
{
public static async Task<bool> CheckForRunningProcess(string processName)
    {
        //Requests permission for app.
        await AppDiagnosticInfo.RequestAccessAsync();
        //Gets the running processes.
        var processes = ProcessDiagnosticInfo.GetForProcesses();
        //Returns result of searching for process name.
        return processes.Any(processDiagnosticInfo => processDiagnosticInfo.ExecutableFileName.Contains(processName));
    }
}

启动非 UWP 应用程序/进程有点脏但可能。

首先,需要一个简单的控制台(非 uwp)应用程序。将以下代码中的 directoryPath 替换为您适用的目录路径。

using System;
using System.Diagnostics;

namespace Launcher
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                if (args.Length != 3) return;
                string executable = args[2];
                string directoryPath = "C:\\Program Files (x86)\\Arduino\\hardware\\tools\\";
                Process.Start(directoryPath + executable);
            }
            catch (Exception e)
            {
                Console.ReadLine();
            }

        }
    }
}

构建控制台应用并将 Launcher.exe 放置在 UWP 应用资产文件夹中。

现在您需要添加运行 Launcher 的功能,为此,请将“runFullTrust”功能添加到 UWP 应用。

<Capabilities>
<Capability Name="internetClient" />
<rescap:Capability Name="runFullTrust" />
<rescap:Capability Name="appDiagnostics" />
</Capabilities>

对于桌面,您还需要在 package.appxmanifest(代码)中添加桌面功能和扩展。

xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
IgnorableNamespaces="uap mp rescap"

然后在 package.appxManifest 和 inside 的下方。

<Extensions>
    <desktop:Extension Category="windows.fullTrustProcess" Executable="Assets\Launcher.exe" >
      <desktop:FullTrustProcess>
        <desktop:ParameterGroup GroupId="SomeGroup1" Parameters="ProcessName1.exe"/>
        <desktop:ParameterGroup GroupId="SomeGroup2" Parameters="ProcessName2.exe"/>
      </desktop:FullTrustProcess>
    </desktop:Extension>
</Extensions>

最后,添加应用版本所需的“UWP 的 Windows 桌面扩展”引用。

现在您可以调用 Launcher 并启动必要的过程。

public static async void LaunchProcess(int groupId)
    {
        switch (groupId)
        {
            case 1:
                await FullTrustProcessLauncher.LaunchFullTrustProcessForAppAsync("SomeGroup1");
                break;
            case 2:
                await FullTrustProcessLauncher.LaunchFullTrustProcessForAppAsync("SomeGroup2");
                break;
        }
    }

综合以上,一种可能是……

    public enum ProcessResult
        {
            ProcessAlreadyRunning,
            FailedToLaunch,
            SuccessfulLaunch
        }
    public static async Task<ProcessResult> CheckLaunchCheckProcess1()
        {
            if (await CheckForRunningProcess("ProcessName1.exe")) return ProcessResult.ProcessAlreadyRunning;
            LaunchProcess(1);
            return await CheckForRunningProcess("ProcessName1.exe") ? ProcessResult.SuccessfulLaunch : ProcessResult.FailedToLaunch;
        }

这只是如何在单个 uwp 应用程序中完成启动非 uwp 应用程序的示例。对于 Windows 商店应用程序提交,受限功能需要批准,如果被拒绝,可能会延迟或停止部署。

如果调用应用程序和启动应用程序都是 uwp 并且由您编写,则适当的解决方案可能是使用 URI 进行应用程序之间的通信,MS doc 链接Launch an app with a URI


推荐阅读