打开当前应用程序中的另一个应用程序

时间:2019-03-11 15:54:59

标签: c# unity3d uwp

我们在Windows Store中有一个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应用程序中的注册信息。我知道使用桥接器等方法有很多,但是我不知道如何以及从哪里开始。

1 个答案:

答案 0 :(得分:0)

我遇到了类似的情况,但是我正在检查应用程序是否正在运行,如果没有运行,请启动它。在我的情况下,我想检查并启动的应用既不是我编写的,也不是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应用程序资产文件夹中。

现在,您需要添加运行启动器的功能,然后向UWP应用添加“ runFullTrust”功能。

<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和内部的下面。

<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桌面扩展”引用。

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

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

相关问题