从 C# Windows 桌面应用程序执行命令提示符查询 [英] Execute command prompt query from C# Windows Desktop App

查看:26
本文介绍了从 C# Windows 桌面应用程序执行命令提示符查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在早期版本的 Visual Studio 2013 Pro - C# Forms App 中运行此代码,并且运行良好.但是,我现在已经下载了 Visual Studio 2017 社区,并且相同的代码不再起作用.它只是打开命令提示符空白窗口,不执行任何命令.

I am running this code in previous versions of Visual Studio 2013 Pro - C# Forms App, and it is working perfectly fine. However I have now downloaded the Visual Studio 2017 Community and the same code is not working anymore. It is just opening the command prompt blank window and doesn't execute any commands.

此外,它现在甚至不需要任何管理员权限.

Also, it is not even asking for any admin privileges now.

任何人都可以提出建议,我应该如何在社区版中使用它.谢谢.

Can anyone please suggest, how should I get this working in community edition. Thanks.

        Process p1 = new Process();
        p1.StartInfo.FileName = "cmd.exe";
        p1.StartInfo.Arguments = "java";
        p1.StartInfo.UseShellExecute = false;
        p1.StartInfo.RedirectStandardOutput = true;
        p1.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
        p1.StartInfo.CreateNoWindow = false;
        p1.StartInfo.Verb = "runas";
        p1.Start();
        p1.StandardOutput.ReadToEnd();

推荐答案

您的论点中似乎缺少/c

It looks like you are missing /c from your arguments

p1.StartInfo.Arguments = "/c java";

p1.StartInfo.Arguments = "/c java";

  Process p1 = new Process();
        p1.StartInfo.FileName = "cmd";
        p1.StartInfo.Arguments = "/c java";
        p1.StartInfo.UseShellExecute = false;
        p1.StartInfo.RedirectStandardOutput = true;
        p1.StartInfo.RedirectStandardError = true;
        p1.StartInfo.Verb = "runas";

        p1.Start();

        StringBuilder sb = new StringBuilder();
        while (!(p1.StandardOutput.EndOfStream))
             sb.Append($"{ p1.StandardOutput.ReadLine()}");


        while (!(p1.StandardError.EndOfStream))
            sb.Append($"{ p1.StandardError.ReadLine()}");

        p1.WaitForExit();

这篇关于从 C# Windows 桌面应用程序执行命令提示符查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆