如何在使用c#的空间的控制台应用程序中传递参数

时间:2013-12-23 06:12:33

标签: c# sql-server console-application

我有一个控制台文件,它有6个参数 enter image description here

我使用C#应用程序传递此参数,代码

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 !!");  
}

它的工作完美,但每当我传递其中有空格的参数时,就像在备份文件夹路径中的此代码中一样,我传递文件夹路径 string backupPath = @“D:\ backup \ Smart Tracker \”< /强> 所以,它不起作用,控制台应用程序将空间视为参数的结尾, 并显示此错误.. enter image description here

那么,我怎样才能传递有空间的论点!!!!

5 个答案:

答案 0 :(得分:4)

将您的路径包含在single quotes中,将整个路径视为单个字符串argument

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

答案 1 :(得分:3)

将间隔参数封装在引号中。 I.E. @“\”D:\ backup \ Smart Tracker \“”;

答案 2 :(得分:0)

尝试使用Environment.CommandLine,你必须解析它或用引号括起来。

答案 3 :(得分:0)

使用single single quotes ' '

逃离路径
string backupPath = @"'D:\backup\Smart Tracker\'";

或者如果您愿意:

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

答案 4 :(得分:0)

我尝试下面的代码,它的工作完美!

 char c = Convert.ToChar(34);
 string backupPath = c + @"D:\backup\Smart Tracker" + c;