Click here to Skip to main content
14,835,369 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
See more:
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();
Posted
Updated 2-Feb-20 17:58pm

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

There is a fuller explanation here: How to Execute a Command in C# ?[^]
   
another solution:

C#
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);
        }
    }
}
   
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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900