3

My scenario in C# project is user will pass path like "c:\homedir\mydir" to batch file then batch file should accept this path and create directory at the specified path.

I don't know how to pass string to batch file through c# and how batch file will accept string and process it.

3
  • How to send series of commands to a command window process? Commented Apr 1, 2013 at 13:18
  • Why pass parameter to batch file ? You can't process inside C# app ? It complicates things like getting feedback from batch file. How will you know if something went wrong ? You can use status/result codes but that is not very robust. Commented Apr 1, 2013 at 13:19
  • Hi Peter Thanks for your suggestions, I will try to do the same from c# application. Commented Apr 2, 2013 at 6:07

2 Answers 2

2

Create a process and pass your argument(s) through the StartInfo.Arguments property.

Process proc = new Process();
proc.StartInfo.FileName = //path to your BAT file
proc.StartInfo.Arguments = String.Format("{0}", @"C:\homedir\mydir");
//set the rest of the process settings
proc.Start();

That will load your BAT file and pass whichever arguments you've added. Your BAT file can access the arguments using %1 for the first argument, %2 for the second one etc...

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

2 Comments

Maybe add some info how will C# code know that batch process ended. What to do if it loops indefinitely ? BTW, before even start new process check if given dir exists.
Fair comment but my answer assumes OP will take some responsibility with error handling. There aren't enough details in OP's post to make it worth trying to cover all possible aspects.
1

Since you didn't gave us any information, I just give an example of these subjects.

First of all, you need to use Process class include System.Diagnostics namespace.

Provides access to local and remote processes and enables you to start and stop local system processes.

An example with a batch file:

 Process p = new Process();
 p.StartInfo.UseShellExecute = false;
 p.StartInfo.RedirectStandardOutput = true;
 p.StartInfo.FileName = "yourbatchfile.bat";
 p.Start();

For passing arguments, you can use ProcessStartInfo.Arguments property.

Gets or sets the set of command-line arguments to use when starting the application.

1 Comment

Thanks Soner,It is helpful for me

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.