本文介绍了如何运行命令提示符命令并在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();

推荐答案

startInfo.Arguments = "/c dir";



这里有一个更全面的解释: []


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);
        }
    }
}



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

11-01 18:48