如何运行命令提示符命令并在Windows 7中的c#中获取输出 [英] How to run command prompt command and get output in c# in windows 7

查看:92
本文介绍了如何运行命令提示符命令并在Windows 7中的c#中获取输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的代码来运行命令提示符和列出目录文件。它运行命令提示符但dir命令没有执行。请告诉我问题出在哪里。



System.Diagnostics.Process process = new System.Diagnostics.Process();

System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

startInfo.UseShellExecute = false;

startInfo.RedirectStandardOutput = true;

startInfo.FileName =CMD.exe;

startInfo.Arguments =dir;

process.StartInfo = startInfo;

process.Start();

string output = process.StandardOutput.ReadToEnd( );

MessageBox.Show(输出);

process.WaitForExit();

I used below code to run command prompt and list directory files .Its running command prompt but "dir" command not getting execute.Please tell me where is the problem.

System.Diagnostics.Process process = new System.Diagnostics.Process();
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = "CMD.exe";
startInfo.Arguments = "dir";
process.StartInfo = startInfo;
process.Start();
string output = process.StandardOutput.ReadToEnd();
MessageBox.Show(output);
process.WaitForExit();

推荐答案

简单:将你的论点改为:

Simple: change your arguments to:
startInfo.Arguments = "/c dir";



这里有一个更全面的解释:如何在C#中执行命令? [ ^ ]


另一种解决方案:



another solution:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Test_CmdExec
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (Process p = new Process())
                {
                    // set start info
                    p.StartInfo = new ProcessStartInfo("cmd.exe")
                    {
                        RedirectStandardInput = true,
                        UseShellExecute = false,
                        WorkingDirectory = @"d:\"
                    };
                    // event handlers for output & error
                    p.OutputDataReceived += p_OutputDataReceived;
                    p.ErrorDataReceived += p_ErrorDataReceived;

                    // start process
                    p.Start();
                    // send command to its input
                    p.StandardInput.Write("dir" + p.StandardInput.NewLine);
                    //wait
                    p.WaitForExit();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        static void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            Process p = sender as Process;
            if (p == null)
                return;
            Console.WriteLine(e.Data);
        }

        static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            Process p = sender as Process;
            if (p == null)
                return;
            Console.WriteLine(e.Data);
        }
    }
}


我没有完整的答案。如果我得到它,那么我会报告回来。



但是我在控制台应用程序中尝试代码时发现的很快就是当你调用这样的cmd.exe时,cmd在cmd中打开了Visual工作室运行时调试应用程序。在F5模式下,我不得不输入两次退出应用程序。希望我在那里有意义。



所以,这意味着参数dir不是发送命令的正确方法。
I don't have the full answer. If I get it, then I will report back.

But what I found out quickly by trying your code in a console app is that when you invoke cmd.exe like this then the cmd is open inside the cmd that Visual studio runs while debugging the app. I had to type exit twice to come out of the app while in F5 mode. Hope I made sense there.

So, what that means is that the argument "dir" is not the right way to send the command.


这篇关于如何运行命令提示符命令并在Windows 7中的c#中获取输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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