12

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:

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

13 Comments

When I use this above both method my batch file run exe, according to its own value, i mean it did not take values from this arguments, so, is there any change require in batch file ??? currently my batch file is same as showing upper image, I give different last arguments in code "E:\" but still its follow batch file path.
@VARUNNAYAK: actually it works ,ok let me check it .
@VARUNNAYAK : did you check it? it is working for me.
No its not, See, last argument its where to save this backup file, I give "D:\Test" in batch file, but in code i give it "E:\", And its store backup in "D:\TEST", Its simply run batch file didnt pass arguments
I also add that screen shot in last image.. Check it
|
4

You are missing a format for your String.Format call.

proc.StartInfo.Arguments should be more like

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

However, keep in mind that your arguments could contain whitespaces. I would do this.

var args = new string[] { consolepath,Pc,database,UserName,Password,"F",bacPath };
var startupInfo = String.Join(" ", args.Select(x => "\"" + x + "\""));

1 Comment

{7} is not required as there are only 7 arguments.

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.