0

I have a console file, which takes 6 arguments enter image description here

I pass this argument using C# application, below code

try
{
    intializeConnections();
    string consolepath =    System.Configuration.ConfigurationManager.AppSettings["ConsoleApp_backup"].ToString().Trim();
    // string backupPath = System.Configuration.ConfigurationManager.AppSettings["DataBase_BackupPath"].ToString().Trim();
    string backupPath = @"D:\backup\Smart Tracker\";
    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo.FileName = consolepath;
    // proc.StartInfo.Arguments = String.Format(consolepath, Pc, database, UserName, Password, "F", bacPath);
    proc.StartInfo.Arguments = String.Format("{0} {1} {2} {3} {4} {5}", serverName, DatabaseName, UserId, pw, "F",backupPath );
    //set the rest of the process settings
    proc.Start();

    clsGlobleFunction.InsertAuditTrailRecord_new(this.Name, "", "", "FullBackup Of Databse Done Sucessfull", clsGlobleFunction.ActiveUser);
    MessageBox.Show("FullBackup Done Successfully!!!!");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message + "Give Correct Path in input !!");  
}

Its work perfectly, but whenever i pass argument which have space in it, like in this code in backup Folder path, i am passing folder path string backupPath = @"D:\backup\Smart Tracker\" so, its not working, console application consider space as a ending of argument, and showing this error.. enter image description here

so, how can i pass argument which have space!!!!

Ben T
  • 4,656
  • 3
  • 22
  • 22
VARUN NAYAK
  • 656
  • 5
  • 15
  • 36
  • As always: [Better use `Exception.ToString()` instead of `Exception.Message`](http://stackoverflow.com/questions/2176707/exception-message-vs-exception-tostring) to get things like call stack. – Uwe Keim Dec 23 '13 at 06:25

5 Answers5

4

enclose your path within single quotes to consider the whole path as single string argument.

string backupPath = @"'D:\backup\Smart Tracker\'";
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
3

Encapsulate the spaced argument in quotations. I.E. @"\"D:\backup\Smart Tracker\"";

Jim
  • 209
  • 3
  • 10
0

Try using Environment.CommandLine, you'll have to parse it or wrap in quotes.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
0

Escape the path with single single quotes ' '

string backupPath = @"'D:\backup\Smart Tracker\'";

Or if you prefer:

string backupPath = @"\"D:\backup\Smart Tracker\"";
Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
0

I try below code and its works perfectly !!!

 char c = Convert.ToChar(34);
 string backupPath = c + @"D:\backup\Smart Tracker" + c;
VARUN NAYAK
  • 656
  • 5
  • 15
  • 36