在Windows 10通用应用平台上运行CMD命令

3

如何通过C#在Windows 10(现代)通用应用程序平台(UWP)上运行CMD命令?我尝试使用以下代码(它适用于Windows窗体应用程序):

using System.Diagnostics;

Process cmd = new Process();
            cmd.StartInfo.FileName = "cmd.exe";
            cmd.StartInfo.RedirectStandardInput = true;
            cmd.StartInfo.RedirectStandardOutput = true;
            cmd.StartInfo.CreateNoWindow = true;
            cmd.StartInfo.UseShellExecute = false;
            cmd.Start();

            cmd.StandardInput.WriteLine("the command");
            cmd.StandardInput.Flush();
            cmd.StandardInput.Close();
            cmd.WaitForExit();
            Console.WriteLine(cmd.StandardOutput.ReadToEnd());

但是我遇到了一个错误:
Severity    Code    Description Project File    Line    Suppression State
Error   CS0246  The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)   OneSpot C:\Users\reuve\Documents\Visual Studio 2015\Projects\app\app\MainPage.xaml.cs   37  Active

并且:

Severity    Code    Description Project File    Line    Suppression State
Error   CS0103  The name 'Console' does not exist in the current context    OneSpot C:\Users\reuve\Documents\Visual Studio 2015\Projects\app\app\MainPage.xaml.cs   49  Active

感谢大家。

这是确切的代码还是其中一些在某个函数中? - crashmstr
3
在UWP应用中,你无法运行可执行文件。 - Mehrzad Chehraz
UWP将在某个时候包括Xbox,我非常有信心地说,在Xbox上无法启动命令提示符。 - zneak
我看到你可以编写仅适用于PC的代码:https://msdn.microsoft.com/zh-cn/library/windows/apps/dn894631.aspx?f=255&MSPPError=-2147217396 - Reuven Herszkowicz
1
@ReuvenHerszkowicz 即使您在PC上找到了一种方法,您的应用程序也无法获得证书以在Windows商店中发布。 - Mehrzad Chehraz
显示剩余2条评论
1个回答

4
你无法从UWP应用程序中启动外部可执行文件。这是由安全模型预防的。 你只能使用Launcher API提供的方法。 你可以使用LaunchFileLaunchUri打开默认应用程序打开的文件。系统将启动用户注册的应用程序来打开该文件。

特别注意事项:您的应用程序无法选择启动的应用程序。用户决定启动哪个应用程序。用户可以选择通用 Windows 平台 (UWP) 应用程序或 Windows 桌面应用程序。如果操作系统自动执行包含代码或脚本的文件类型,例如 .exe、.msi 和 .js 文件,则无法启动它们。 - kayleeFrye_onDeck

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接