C# 调用命令行,参数有空格

 在程序中调用cmd命令打开一个文件,而文件路径带有空格,如果直接把路径传给cmd,那么cmd就会把路径空格前面的部分当做是一个参数,空格后当做另一个参数,命令行执行把后边截掉了,导致程序出错,会弹出了C:\Program 不是内部或外部命令,也不是可运行的程序或批处理文件的错误提示。解决方法是把传入的参数前后添加双引号,如下:

 

  private static void ResxToRes(ArrayList ResxPath)
        {
            //ResxFile 是一个文件夹,用来存放 需要转换的.resx 文件 string s = ""; Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.CreateNoWindow = false; p.Start(); // + '"' + '"' + 可以用 \"\" 替换 // p.StandardInput.WriteLine("%comspec% /k " + '"' + '"' + @"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin" + '"' + '"'); // p.StandardInput.WriteLine("%comspec% /k " + '"' + '"' + @"C:\Program Files\Microsoft Visual Studio 10.0\VC\vcvarsall.bat" + '"' + '"' + " x86 "); // p.StandardInput.WriteLine(@" cd C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin"); for (int i = 0; i < ResxPath.Count; i++) { string Filename = ResxPath[i].ToString(); //根据需要写相应算法生成文件名; ////resgen E:\Filename1.resx e:\ResName1.resources // p.StandardInput.WriteLine("ResGen" + Filename + " " + System.IO.Path.GetFileNameWithoutExtension(Filename) + @".resx");\string string ResName =Path.GetFullPath(Filename)+ ".resx"; string str = "\""+@"C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\ResGen.exe"+"\"" + " " + Filename + " " + ResName; p.StandardInput.WriteLine(str);//参数带有空格 应加上引号处理 p.StandardInput.WriteLine(" "); } //此处要exit两次 //退出visual studio 到 cmd.exe p.StandardInput.WriteLine("exit"); //退出cmd.exe p.StandardInput.WriteLine("exit"); p.WaitForExit(); s = s + p.StandardOutput.ReadToEnd(); p.Close(); // return s; }
 public static string startcmd(string command)
        {
            string output = "";
            try
            {
                Process cmd = new Process();
                cmd.StartInfo.FileName = command;
                cmd.StartInfo.UseShellExecute = false;
                cmd.StartInfo.RedirectStandardInput = true;
                cmd.StartInfo.RedirectStandardOutput = true;
                cmd.StartInfo.CreateNoWindow = true;
                cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                cmd.Start();
                output = cmd.StandardOutput.ReadToEnd();
                Console.WriteLine(output);
                cmd.WaitForExit();
                cmd.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
            return output;
        }

        public static string startcmd(string command, string argument)
        {
            string output = ""; 
            Process cmd = new Process();
            try
            {
              
                cmd.StartInfo.FileName = command;
                cmd.StartInfo.Arguments = argument;
                cmd.StartInfo.UseShellExecute = false;
                cmd.StartInfo.RedirectStandardInput = true;
                cmd.StartInfo.RedirectStandardOutput = true;
                cmd.StartInfo.CreateNoWindow = true;
                cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
                cmd.Start();
                output = cmd.StandardOutput.ReadToEnd();
                Console.WriteLine(output);
                cmd.WaitForExit();
                cmd.Close();
            }
            catch (Exception e)
            {
                cmd.Close();
                Console.WriteLine(e);
            }
            return output;
        }

 

转载于:https://www.cnblogs.com/zuiyirenjian/p/4053388.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值