在程序中调用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;
}

C# 调用命令行,参数有空格的更多相关文章

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

    想用 c#调用如下的DOS命令: C:\Program Files\Common Files\System\DBWatcherInstall\dtexec.exe /f C:\Program File ...

  2. Java:命令行参数的传入和调用

    1.传入 传入时机:执行时 格式: //编译 javac Main.java //执行并传入命令行参数 -version java Main -version 此时,参数-version就以Strin ...

  3. getopt函数的使用——分析命令行参数

    getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...

  4. mysql命令行参数(转)

    MySQL命令行参数 Usage: mysql [OPTIONS] [database] //命令方式  -?, --help //显示帮助信息并退出  -I, --help //显示帮助信息并退出  ...

  5. mysql 命令行参数

    MySQL命令行参数 Usage: mysql [OPTIONS] [database] //命令方式  例如: mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAM ...

  6. 命令行参数(argc, argv)

    每个C语言程序都必须有一个称为main()的函数,作为程序启动的起点.当执行程序时,命令行参数(command-line argument)(由shell逐一解析)通过两个入参提供给main()函数. ...

  7. Python 的命令行参数处理 optparse->argparse

    optaprse自2.7版开始弃用:弃用optparse模块,不会进一步开发,将继续开发argparse模块作为替代. 但是用习惯了optparse,还是很好用的撒. optparse使用起来,相比旧 ...

  8. 【转】getopt分析命令行参数

    (一) 在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如: ./a.out -a1234 -b432 -c -d 程序会根据读取的参数执行相应的操作,在C语言中,这个 ...

  9. getopt(分析命令行参数)

    ref:http://vopit.blog.51cto.com/2400931/440453   相关函数表头文件         #include<unistd.h>定义函数       ...

随机推荐

  1. 【转】Microsoft® SQL Server® 2012 Performance Dashboard Reports

    http://www.cnblogs.com/shanyou/archive/2013/02/12/2910232.html SQL Server Performance Dashboard Repo ...

  2. DRP PK 牛腩新闻发布系统

    一.JSP与ASP (1)Web服务器的支持:大多数通用的Web服务器如:Apache.Netscape和Microsoft IIS都支持JSP页面,只有微软本身的Microsoft IIS和Pers ...

  3. ASP.NET从数据库中取出数据,有数据的复选框为选中

    在KS系统中在更新菜单的时候,当查出菜单的时候要查出菜单下面已经有了哪些界面了我用了一下的方法弄的.代码如下: 界面代码: <%@ Page Language="C#" Au ...

  4. Oracle中的if...then...elsif

    if...then...elsif实现多分支判断语句 其语法如下: if <condition_expression1> then plsql_sentence_1; elseif< ...

  5. WebDev.WebServer40.exe已停止工作

    今天写程序的遇到这个错误 错误的原因是代码中有死循环

  6. [转]unable to resolve superclass of 的奇怪问题和一种解决方法!

    [转]unable to resolve superclass of 的奇怪问题和一种解决方法! http://blog.csdn.net/jackymvc/article/details/90015 ...

  7. 微软职位内部推荐-Sr. Dev Lead

    微软近期Open的职位: JD 如果你想试试这个职位,请跟我联系,我是微软的员工,可以做内部推荐.发你的中英文简历到我的邮箱:Nicholas.lu.mail(at)gmail.com

  8. 微软职位内部推荐-SDE2 (Windows - Audio)

    微软近期Open的职位: SDE2 (Windows - Audio) Windows Partner Enablement team in Operating System Group is loo ...

  9. VBS基础篇 - class

    Class 语句:声明一个类的名称,以及组成该类的变量.属性和方法的定义. Class name '参数name必选项,Class 的名称 statements '一个或多个语句,定义了 Class ...

  10. 关于拓扑排序(topologicalsort)

    假设我们有一组任务要完成,并且有些任务要在其它任务完成之后才能开始,所以我们必须非常小心这些任务的执行顺序.如果这些任务的执行顺序足够简单的话,我们可以用链表来存储它们,这是一个很好的方案,让我们可以 ...