C# 使用 Metro 风格的应用程序启动桌面应用程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9527644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 07:45:23  来源:igfitidea点击:

Launching a Desktop Application with a Metro-style app

c#windowswindows-8microsoft-metro

提问by JacobTheDev

Is there a way to launch a desktop application from a Metro-style app on Windows 8? I'm trying to create some simple shortcuts to desktop applications to replace the desktop icons on the start screen, which look out of place.

有没有办法从 Windows 8 上的 Metro 风格应用程序启动桌面应用程序?我正在尝试为桌面应用程序创建一些简单的快捷方式,以替换开始屏幕上看起来不合适的桌面图标。

I just need something super simple, preferably in C#, to open an application as soon as the app loads. I'm planning on making these shortcuts for some games, photoshop, etc, not anything I've made myself. They're also just for personal use, so I can use direct paths to applications like "C:\Program Files (x86)\Steam\steamapps\common\Skyrim\TESV.exe"

我只需要一些超级简单的东西,最好是在 C# 中,以便在应用程序加载后立即打开应用程序。我打算为一些游戏、photoshop 等制作这些快捷方式,而不是我自己制作的任何东西。它们也仅供个人使用,因此我可以使用应用程序的直接路径,例如"C:\Program Files (x86)\Steam\steamapps\common\Skyrim\TESV.exe"

采纳答案by Niranjan Singh

If you simply want to run a desktop application like (notepad, wordpad, internet explorer etc) then go through Process Methodsand ProcessStartInfo Class

如果您只是想运行桌面应用程序(记事本、写字板、Internet Explorer 等),请通过Process MethodsProcessStartInfo Class

try
{
// Start the child process.
    Process p = new Process();
    // Redirect the output stream of the child process.
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.FileName = "C:\Path\To\App.exe";
    p.Start();
}

// Exp 2

// 经验 2

// Uses the ProcessStartInfo class to start new processes,
// both in a minimized mode.
void OpenWithStartInfo()
{
    ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe");
    startInfo.WindowStyle = ProcessWindowStyle.Minimized;

    Process.Start(startInfo);

    startInfo.Arguments = "www.northwindtraders.com";

    Process.Start(startInfo);
}

On Windows 8 Metro application i discovered this: How to Start a external Program from Metro App.

All the Metro-style applications work in the highly sand boxed environment and there is no way to directly start an external application.

You can try to use Launcher class– depends on your need it may provide you a feasible solution.

在 Windows 8 Metro 应用程序上,我发现了这一点:How to Start a external Program from Metro App

所有 Metro 风格的应用程序都在高度沙盒环境中工作,无法直接启动外部应用程序。

您可以尝试使用Launcher 类- 取决于您的需要,它可能会为您提供可行的解决方案。

Check this:
Can I use Windows.System.Launcher.LauncherDefaultProgram(Uri) to invoke another metro style app?

检查这个:
我可以使用 Windows.System.Launcher.LauncherDefaultProgram(Uri) 来调用另一个 Metro 风格的应用程序吗?

Ref:How to launch a Desktop app from within a Metro app?

参考:如何从 Metro 应用程序中启动桌面应用程序?

Metro IE is a special app. You cannot invoke an executable from Metro style apps.

Metro IE 是一个特殊的应用程序。不能从 Metro 风格应用程序调用可执行文件。

Try this - I have not test yet but may be it will help you..

试试这个 - 我还没有测试过,但它可能会帮助你..

Launcher.LaunchFileAsync

Launcher.LaunchFileAsync

// Path to the file in the app package to launch
string exeFile = @"C:\Program Files (x86)\Steam\steamapps\common\Skyrim\TESV.exe";

var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(exeFile);

if (file != null)
{
    // Set the option to show the picker
    var options = new Windows.System.LauncherOptions();
    options.DisplayApplicationPicker = true;

    // Launch the retrieved file
    bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
    if (success)
    {
       // File launched
    }
    else
    {
       // File launch failed
    }
}

回答by Apollo

I haven't actually tried if it works and it's not really a beautiful solution, but I guess Metro-style apps can launch a URI. You could then create a desktop-program that is registered for a custom URI scheme that would then do the actual program launching.

我还没有真正尝试过它是否有效,也不是一个真正漂亮的解决方案,但我猜 Metro 风格的应用程序可以启动一个 URI。然后,您可以创建一个为自定义 URI 方案注册的桌面程序,然后该程序将执行实际的程序启动。

回答by ghord

What you can do is host external WCF service on your computer with separate installation and connect to it from metro style application using localhost. Then you can do pretty much anything including Process.Start.

您可以做的是通过单独安装在您的计算机上托管外部 WCF 服务,并使用本地主机从 Metro 风格应用程序连接到它。然后你几乎可以做任何事情,包括 Process.Start。

回答by Christoph Weller

I found a solution which is suitable for me. I just made an empty textfile in my app and called it launcher.yourappyouwanttostartand then executed it with

我找到了一个适合我的解决方案。我只是在我的应用程序中创建了一个空文本文件并调用它launcher.yourappyouwanttostart然后执行它

Windows.System.Launcher.LaunchFileAsync("launcher.yourappyouwanttostart");

On the first startup it asks you for the assocation for this file and then you choose the exe file you want to run and from now on every time you execute this file, your app will be started.

在第一次启动时,它会要求您提供此文件的关联,然后您选择要运行的 exe 文件,从现在开始,每次执行此文件时,您的应用程序都会启动。

回答by Andrea Antonangeli

I love simple things, so my solution was to use this:

我喜欢简单的事情,所以我的解决方案是使用这个:

Process.Start("explorer", "shell:AppsFolder\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe!App")

This will start the "new" Sticky Notes coming with Anniversary Update to Windows 10, but it works with all other "Metro" apps I tested. To find the name of the metro app, from Windows Explorer you have to find it in shell:appsfolder using the AppUserModelId column.

这将启动 Windows 10 周年更新附带的“新”粘滞便笺,但它适用于我测试过的所有其他“Metro”应用程序。要查找 Metro 应用程序的名称,您必须从 Windows 资源管理器中使用 AppUserModelId 列在 shell:appsfolder 中找到它。