1

I have two batch files to execute SFTP commands.

  1. Inner batch file
  2. Outer batch file

Inner batch file contains below code :

put D:\Source_Data\SFTP\Sample.xml

Outer batch file contains below code:

psftp -b D:\Source_Data\SFTP\Innerbatch.bat -l username -i D:\WebCash\privatekey.ppk @server.domain.com

Outer batch file internally calls inner batch file. How to execute the batch files using C# code?

Also I need to get the error code (i.e) whether the batch file is executed successfully or not in C# code.

I used the below code to execute the batch files.

Process p = null;
p = Process.Start(@"C:\Batch\file.bat");
p.WaitForExit(1000);
p.ExitCode;

but getting error. Required info does not be determined while process is executing.

Problem is Windows Service. I can able to run the batch file through normal Windows form application. But when I used the same code in Windows Service the process is not exiting.

8
  • 2
    Check out the System.Diagnostics.Process class. Commented Oct 13, 2014 at 13:59
  • I tried but my process is not finishing. Edited the question with my code. pls refer. Commented Oct 13, 2014 at 14:15
  • Should Process.Start(@"Batchfilepath"); be Process.Start(Batchfilepath);? Commented Oct 13, 2014 at 14:22
  • yes.. typo error. My code is Process.Start(@"C:\Batch\File.bat") Commented Oct 13, 2014 at 14:26
  • If you execute the batch files from the Window's run command, do they run well? In how much time? Do the batch files require elevated privilidges? Commented Oct 13, 2014 at 14:36

1 Answer 1

3

You should be using System.Diagnostics.Process for this.

Based on your comments, the batch file can take up to 2 minutes to execute. I would implement the following code:

public static int ExecuteBatchFile(string batchFilePath, int timeout, bool killOnTimeout = false)
{
   using (var p = Process.Start(batchFilePath))
   {
       p.WaitForExit(timeout);

       if (p.HasExited)
           return p.ExitCode;

       if (killOnTimeout)
       {
           p.Kill();
       }
       else
       {
           p.CloseMainWindow();
       }

       throw new TimeoutException(string.Format("Time allotted for executing `{0}` has expired ({1} ms).", batchFilePath, timeout));
   }
}

UPDATE If you are executing the batch file form a windows service, then normally you have to disable the user interface. You can do so following this pattern:

ProcessStartInfo info = new ProcessStartInfo(exePath); //exePath must be full path.
info.CreateNoWindow = true;
info.UseShellExecute = false;
Process.Start(info);
Sign up to request clarification or add additional context in comments.

7 Comments

I have tried those codes already. Problem is When i execute the batch file through Windows command prompt it is running with in 1 min. But if i execute through code it is not finishing. I dont know how long it takes to execute though code. I waited for nearly 5 mins.
@user3859666 Again, do you need special privilidges to run the batch file? Admin rights? Process.Start is equivalent to the window's run command, there shouldn't be any difference.
No.. I have run the Batch file and Code in same Machine. I did not give any access previledges while running the command through Windows run.
Problem is Windows Service. I can able to run the batch file through normal Windows form application. But when i used the same code in Windows Service the Process is not Exiting. Pls help.. Its Urgent
@user3859666 See update to answer. Hope it helps. You should have said it was a window service.
|

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.