6

I want to run a cmd and run some command in it. I wrote this code:

Process p = new Process();
ProcessStartInfo info =new ProcessStartInfo();

info.FileName = "cmd.exe";
info.WorkingDirectory = this.workingDirectory;
info.RedirectStandardInput = true;
info.UseShellExecute = false; 
info.CreateNoWindow = true;
p.StartInfo = info;

var x=p.Start();
using (StreamWriter sw = p.StandardInput)
{
    if (sw.BaseStream.CanWrite)
    {
        sw.WriteLine(@"set path=c:\temp"+ ";%path%");
        sw.WriteLine(@"@MyLongproces.exe");
    }
}

But it doesn't work:

  1. I can not see command window (even when I set info.CreateNoWindow to false).
  2. My command is not running.

What is the problem? and how can I fix it?

  • Update1

This code doesn't work:

  string binDirectory = Path.Combine(FileSystem.ApplicationDirectory, this.binFolderName);
  ProcessStartInfo info = new ProcessStartInfo("cmd", @"/c " + Path.Combine(binDirectory, command));
  info.RedirectStandardInput = false;
  info.RedirectStandardOutput = true;
  info.UseShellExecute = false;
  info.CreateNoWindow = false;
  System.Diagnostics.Process proc = new System.Diagnostics.Process();
  proc.StartInfo = info;
  proc.Start();
  string result = proc.StandardOutput.ReadToEnd();

No cmd window is shown and result it "".

But this code works:

     Process.Start(Path.Combine(binDirectory, command));

The problem with above code is:

  1. I can not define the working directory.
  2. It shows a CMD window when I don't want it to show.

Any idea why it is not working?

Igor Ševo
  • 5,459
  • 3
  • 35
  • 80
mans
  • 17,104
  • 45
  • 172
  • 321

8 Answers8

1

You are setting the CreateNoWindow option:

info.CreateNoWindow = true;

ProcessStartInfo.CreateNoWindow - true if the process should be started without creating a new window to contain it; otherwise, false. The default is false.

CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
0

You still need to tell your process what command you want to run. In this case, it sounds like you want it to start cmd.exe

metalhead
  • 558
  • 1
  • 10
  • 24
0

Try that:

Before

 p.StartInfo = info;

Insert

info.Arguments = "/c ping www.google.com.br"; //command here
Moondustt
  • 864
  • 1
  • 11
  • 30
0

Why you write dificult things, here is how to run commands :

try {

    System.Diagnostics.ProcessStartInfo procStartInfo =
        new System.Diagnostics.ProcessStartInfo("cmd", "/c " + command);

    StreamReader.procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;

    procStartInfo.CreateNoWindow = true;

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();

    string result = proc.StandardOutput.ReadToEnd();

    Console.WriteLine(result);
  }
  catch (Exception objException)
  {
        // Log the exception
  }
Obama
  • 2,586
  • 2
  • 30
  • 49
0

I guess you are trying to run MyLongProcess.exe here. If yes, there is no need to launch command prompt. You can try the following:

//Create process info and set arugments here and then assign it to process object
// If the exe does not expect any arguments, simply use line below
Process process = Process.Start("MyLongProcess.exe");
danish
  • 5,550
  • 2
  • 25
  • 28
0

You could try this

//Get the paths list and add your custom path to it
string paths = System.Environment.GetEnvironmentVariable("PATH") + @";C:\temp";

//Create a array consisting of all the paths
string[] pathArray = paths.Split(';');

//Search the paths for the first path in which your exe is present
string exePath = pathArray.Select(x => System.IO.Path.Combine(x, "MyLongproces.exe"))
                          .Where(x => System.IO.File.Exists(x))
                          .FirstOrDefault();

if (string.IsNullOrWhiteSpace(exePath) == false)
{
    //start your exe
    System.Diagnostics.Process.Start(exePath);
}
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
0

Althought this is pretty old, in my opinion the problem lies within the specified parameter for /c of cmd. If you specify an argument with spaces in it you need to use the " character at the start and the end. So I would suggest changing this

ProcessStartInfo info = new ProcessStartInfo("cmd", @"/c " + Path.Combine(binDirectory, command));

to this.

ProcessStartInfo info = new ProcessStartInfo("cmd", string.Format("/c \"{0}\"", Path.Combine(binDirectory, command)));
Markus Safar
  • 6,324
  • 5
  • 28
  • 44
0

try to add this line

info.Verb = "runas";
Ateeq
  • 797
  • 2
  • 9
  • 27