Monday, January 28, 2008

C#: Execute a windows batch script and wait for return

Unlike simply calling Process.Start("executable_name.exe","arguments") - which does not pause the parent C# process thread, use the below implementation for waiting for the batch script to finish executing before proceeding with the next set of instructions:

using System.Diagnostics;

Process proc = new Process();
proc.StartInfo.FileName = "C:\\myscript.bat";
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.Start();
proc.WaitForExit();
int exitCode = proc.ExitCode;
proc.Close();

11 comments:

  1. Great Job Dude Keep Going

    ReplyDelete
  2. Millions of thanks, is just what I need

    ReplyDelete
  3. Hi KC,

    A simple question.

    what is the difference between Uri and Url in C#.
    What is the equivalent of URL(JAVA) in C#

    Thanks in Advance

    ReplyDelete
  4. Hello Kaushik,

    I am Poornima from bangalore, i am working on an application which needs to convert the typed text from enlish to hindi. I tried using google transliterate in my app, it worked fine. It requires internet and my app should not use net...so i searched the web and got 2 js files one is hindi.js which contains codes for each letter. And the other one is converter.js which contains some functions. I want use bnoth the js files my app. Please help me..i can send you the files for reference.

    Thanks & Regards,
    Poornima.

    ReplyDelete
  5. Interesting but how can I let my batch file return a value?

    ReplyDelete
  6. Hi, I am using a file converter and then the converted file is used in some other process.

    But the problem is that it gives error cause the second process starts before the converting complete.

    I used while !file.exist for the converted file but in that case the while process takes over the converting process and it never gets converted and get into infinite loop of while.

    Please suggest me how to do that.

    Thanks,
    Gunjan

    ReplyDelete
  7. Gunjan,
    See if this works for you: http://kseesharp.blogspot.com/2009/05/c-execute-two-dependant-processes-2.html

    ReplyDelete
  8. Brilliant exactly wat i needed. previouslu i used to open the cmd and then write each line.. this is much better

    ReplyDelete
  9. KC,

    I've been asking around with no answer, but I want to call a PHP script from an ASP C# application, passing some variables in the URL as I do so. Is there an easy way of doing this? I don't need a response, I simply need to get the webserver to process the file.

    Thanks!

    ReplyDelete
  10. How would I use your code to call a batch file AND pass an argument to it?

    before I used: System.Diagnostics.Process.Start(@"C:\test.bat",selectedItem.ToString());

    where do I but the argument using your code?

    Thanks!

    ReplyDelete
  11. Cool, this helped me a lot. Thanks.

    ReplyDelete

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.