zoukankan      html  css  js  c++  java
  • c#调用命令行遇到带空格的路径

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

     C:Program FilesCommon FilesSystemDBWatcherInstalldtexec.exe /f  C:Program FilesCommon FilesSystemDBWatcherInstallToMSDE.dtsx  /conf C:Program FilesCommon FilesSystemDBWatcherInstallToMSDE.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 FilesCommon FilesSystemDBWatcherInstalldtexec.exe" /f "C:Program FilesCommon FilesSystemDBWatcherInstallToMSDE.dtsx" /conf "C:Program FilesCommon FilesSystemDBWatcherInstallToMSDE.xml" 

    测试失败...

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

     @"C:Program FilesCommon FilesSystemDBWatcherInstalldtexec.exe" /f "C:Program FilesCommon FilesSystemDBWatcherInstallToMSDE.dtsx" /conf "C:Program FilesCommon FilesSystemDBWatcherInstallToMSDE.xml" /de 



  • 相关阅读:
    Oracle 添加主键和索引
    Oracle中查询主键、外键、sequence、表基本信息等
    Spring工作原理
    Ehcache 缓存使用
    socket编程-java
    oracle触发器详解
    单例模式的几种写法
    [LeetCode] 412. Fizz Buzz 嘶嘶嗡嗡
    LeetCode Top Interview Questions
    [LeetCode] 131. Palindrome Partitioning 回文分割
  • 原文地址:https://www.cnblogs.com/onmyway20xx/p/3626346.html
Copyright © 2011-2022 走看看