【问题标题】:how to pass argument in console application which having space using c#如何使用c#在有空间的控制台应用程序中传递参数
【发布时间】:2014-01-11 08:12:28
【问题描述】:

我有一个控制台文件,它有 6 个参数

我使用 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\" 所以,它不起作用,控制台应用程序将空间视为参数的结尾, 并显示此错误..

那么,我如何传递有空格的参数!!!

【问题讨论】:

标签: c# sql-server console-application


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    用引号括起带空格的参数。 IE。 @"\"D:\backup\Smart Tracker\"";

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 如果我说是,你会相信我吗?
      【解决方案4】:

      single single quotes ' '转义路径

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

      或者,如果您愿意:

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

      【讨论】:

      • 感谢您的关注,我都尝试了,但没有成功
      【解决方案5】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-03
        • 2021-04-06
        • 1970-01-01
        • 2012-05-08
        • 2016-05-15
        • 2011-09-03
        • 1970-01-01
        • 2018-01-01
        相关资源
        最近更新 更多