Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameters to a batch file using c#

Tags:

c#

batch-file

I have a console file, which takes 6 arguments

enter image description here

To run this exe, I create one batch file,

enter image description here

Now, I need to send this parameter to the batch file from my one Windows application. This is the code:

         string consolepath = @"E:\SqlBackup_Programs\console-backup\Backup_Console_App";
            string Pc = "VARUN-PC";
            string database = "Smart_Tracker";
            string UserName = "sa";
            string Password = "admin@12";
            string bacPath = @"D:\TEST";

            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = System.Configuration.ConfigurationManager.AppSettings["BATCH_FULLBACKUP"].ToString().Trim();
            proc.StartInfo.Arguments = String.Format(consolepath,Pc,database,UserName,Password,"F",bacPath);
            //set the rest of the process settings
            proc.Start();

But its not working. I tried to change my Batch file like,

@echo off %1 %2 %3 %4 %5 %6 %7

@echo off

but that didn't work either.

Error Image:

like image 661
VARUN NAYAK Avatar asked Dec 17 '13 06:12

VARUN NAYAK


1 Answers

Arguments should be seperated by space.

Method 1:

proc.StartInfo.Arguments =consolepath+" "+Pc+" "+database+" "+UserName+" "+Password+" "+"F"+" "+bacPath;

Method 2: using String.Format()

proc.StartInfo.Arguments =String.Format("{0} {1} {2} {3} {4} {5} {6}",consolepath,Pc,database,UserName,Password,"F",bacPath);  

Solution 2: you should not hardcode the parameter values in batch file

Try This: change the Batch file as below

%1 %2 %3 %4 %5 %6 %7
like image 174
Sudhakar Tillapudi Avatar answered Sep 22 '22 06:09

Sudhakar Tillapudi