c#调用命令行遇到带空格的路径

   想用 c#调用如下的DOS命令:

 C:\Program Files\Common Files\System\DBWatcherInstall\dtexec.exe /f  C:\Program Files\Common Files\System\DBWatcherInstall\ToMSDE.dtsx  /conf C:\Program Files\Common Files\System\DBWatcherInstall\ToMSDE.xml


代码如下:

 

    //用于执行DOS命令的方法
        public static string ExecuteDosCommand(string dosCommand, int milliseconds)
        {
            string output = "";     //输出字符串
            if (dosCommand != null && dosCommand != "")
            {
                Process process = new Process();     //创建进程对象
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";      //设定需要执行的命令
                startInfo.Arguments = "/C @" + dosCommand;   //设定参数,其中的“/C”表示执行完命令后马上退出
                getLogHelper().saveLog(startInfo.Arguments.ToString(), Application.StartupPath + @"\error.log");
                startInfo.UseShellExecute = false;     //不使用系统外壳程序启动
                startInfo.RedirectStandardInput = false;   //不重定向输入
                startInfo.RedirectStandardOutput = true;   //重定向输出
                startInfo.CreateNoWindow = true;     //不创建窗口
                process.StartInfo = startInfo;
                try
                {
                    if (process.Start())       //开始进程
                    {
                        output = process.StandardOutput.ReadToEnd();//读取进程的输出
                    }
                }
                catch
                {
                    throw;
                }
                finally
                {
                    if (process != null)
                        process.Close();
                }
            }
            return output;
        }

一开始把命令拼接成:

   

 "C:\Program Files\Common Files\System\DBWatcherInstall\dtexec.exe" /f "C:\Program Files\Common Files\System\DBWatcherInstall\ToMSDE.dtsx" /conf "C:\Program Files\Common Files\System\DBWatcherInstall\ToMSDE.xml" 

测试失败...

   经过多次试验之后, 使用以下方式可以实现:即开头加一个@同时使用""将带空格的路径包围起来

 @"C:\Program Files\Common Files\System\DBWatcherInstall\dtexec.exe" /f "C:\Program Files\Common Files\System\DBWatcherInstall\ToMSDE.dtsx" /conf "C:\Program Files\Common Files\System\DBWatcherInstall\ToMSDE.xml" /de 



 

posted on 2013-12-24 15:53  babyblue  阅读(2316)  评论(0编辑  收藏  举报