How to invoke command line from C#?

Hi,

I wish to know is there anyway to invoke command line from C# programatically

Regards,

Leo



Answer this question

How to invoke command line from C#?

  • Chris Goedde

    Thanks alot. One last question. I wish to know if there's actually anyway to actually capture the output result of the command line. Like for example.. normally when my command is end successfully it produce 0 as success. If not it will produce some other error codes

    [WebMethod]

    public string Monochrome(string FileName, string Output)

    {

    String output = "";

    System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"C:\magickBatch\monochrome.bat");

    psi.RedirectStandardOutput = true;

    psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

    psi.UseShellExecute = false;

    psi.Arguments = FileName + " " + Output;

    System.Diagnostics.Process listFiles;

    listFiles = System.Diagnostics.Process.Start(psi);

    System.IO.StreamReader myOutput = listFiles.StandardOutput;

    listFiles.WaitForExit(20000);

    if (listFiles.HasExited)

    {

    output = myOutput.ReadToEnd();

    }

    else

    {

    output = "fail";

    }

    return output;

    }

    For my current code, it doesn't able to capture that kind of information.


  • Xiaobo Gu

    Take a look at Process Class

    here are some examples:

    Process myProcess = new Process();

    // Get the path that stores user documents.
    string myDocumentsPath =
    Environment.GetFolderPath(Environment.SpecialFolder.Personal);

    myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc";
    myProcess.StartInfo.Verb = "Print";
    myProcess.StartInfo.CreateNoWindow = true;
    myProcess.Start();\

    ================================

    ProcessStartInfo startInfo = new ProcessStartInfo
    ("<someprogram>.exe");
    startInfo.WindowStyle = ProcessWindowStyle.Minimized;
    startInfo.FileName = filename;
    startInfo.Verb = "Print";
    Process.Start(startInfo);

    =================================

    if you need something more sophisticated take a look at article CommandLineHelper class to launch console applications and capture their output.

    hope this helps



  • CS05pp2

    you do not need this line - Process flip = new Process(); because Start method is static

    be careful with paths . Try following:

    Process.Start(@"c:\<Path To convert.exe>\convert C:\test.jpg –flip c:\inverted.jpg");

    This is because your current path is application execution folder and you have folders in environment variable PATH. I think if you specify full paths everything will be fine.

    Hope this helps



  • KerryLW

    I got this problem when I use standard output.

    Ok I got this batch file with the content

    identify -format "%%w|%%h" %1

    And it is use to identify the width and height of an image

    And when I use

    fileSize = System.Diagnostics.Process.Start(proc2);

    System.IO.StreamReader processOutput = fileSize.StandardOutput;

    fileSize.WaitForExit(20000);

    if (fileSize.HasExited)

    {

    result.exitStatus = processOutput.ReadToEnd();

    }

    I got this

    identify -format "%w%h" test.jpg 160|120

    All I want in fact is just the width and height of the image "160|120" only. I am not too sure why the batch file is actually echo the command line I am using as well. I don't need "identify -format "%w%h" test.jpg" is there anyway to actually get just the return value from the command line


  • MarlAtkins

    you are right - to get exit code you have to use Process.ExitCode Property

    for getting output use Process.StandardOutput Property

    There are good samples.

    HTH



  • Mateusz Rajca

    I can't seems to invoke this imagemagick command

    public void Flip(string FileName)

    {

    Process flip = new Process();

    Process.Start("convert C:\test.jpg –flip inverted.jpg");

    }

    If the command is not particular DOS command like cd and dir etc etc.. Do I need to do anything special to invoke them correctly And for the switches(parameters) like -flip and so on do I need to do anything to pass the parameters correctly

    By the way, I have installed the imagemagick software correctly on my Windows XP and it is working fine from command line.


  • jason duncan

  • Admann

    Sadly the article is very confusing to me. I can get the exit code and so on if the command line got error.

    That is

    if (process.HasExited)

    {

    process.ExitCode.ToString();

    }

    Is there any straight forward way to get the return values from a process For example if I call a kind of dir *.* ... I wish to get the directories values from the process.


  • weiqj

    Thanks. I will try it out.
  • How to invoke command line from C#?