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?

2
  • You have no argument, add one like @"/k"; Commented Apr 16, 2013 at 12:30
  • If you only want to open a directory, you can start a process with the directory path, you don't need to exec cmd in order to do it. Commented Apr 16, 2013 at 16:23

8 Answers 8

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

Try that:

Before

 p.StartInfo = info;

Insert

info.Arguments = "/c ping www.google.com.br"; //command here

Comments

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

Comments

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

Comments

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
  }

Comments

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

Comments

0

try to add this line

info.Verb = "runas";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.