在程序中调用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. Python实战(2)

    在安装python第三方插件库的时候遇到了这个错误 遇到这种问题可以”转战“国内的第三方镜像,问题便可迎刃而解.例如豆瓣镜像——http://pypi.douban.com/simple/ 先安装ea ...

  2. 微软职位内部推荐-Senior Software Development En

    微软近期Open的职位: Job Title: Senior Development Engineer Division: Visual Studio China - Developer Divisi ...

  3. SSMS 2008R2没有智能感知方法解决

    有时SSMS会莫明奇妙的没有了智能感知(前一天还是有的, 第2天就没有了) 在网上查到有如下原因: 1. 服务器上有Offline的DB 解决方案: 将Offline的DB删掉或者设成online即可 ...

  4. Ubuntu系统下USB转串口的使用

    PC系统是Ubuntu12.04,与路由器开发板之间用USB转串口线连接. 一.硬件连接 确认Ubuntu对USB转串口设备的支持. 1.# lsmod | grep usbserial如果有usbs ...

  5. Tesla为什么要公开专利

    这是今天在网上看到Tesla公司的专利墙图片,还是比较有视觉冲击力的,正好可以转来当配图. 业界先锋Tesla日前惊世骇俗地公开电动汽车专利,赢得如潮好评:不过大家都知道,对于西方科技公司,专利历来是 ...

  6. 2463: [中山市选2009]谁能赢呢?- BZOJ

    Description小明和小红经常玩一个博弈游戏.给定一个n×n的棋盘,一个石头被放在棋盘的左上角.他们轮流移动石头.每一回合,选手只能把石头向上,下,左,右四个方向移动一格,并且要求移动到的格子之 ...

  7. javaZIP压缩文件

    问题描述: java ZIP压缩文件 问题解决:     说明:         防止创建压缩文件中中文乱码,需要导入的包:     (1)创建ZipOutputStream 注:     以上引用o ...

  8. javaee web项目的目录结构

    一般web系统的目录结构如下,其中src下的包结构和webroot下(除meta-inf和web-inf)的文件夹可以根据自己的项目实际调整,但是javaee规范的大目录结构不会变: META-INF ...

  9. 树形dp求树的重心

    Balancing Act http://poj.org/problem?id=1655 #include<cstdio> #include<cstring> #include ...

  10. vs2010 mvc3创建的razor引擎模板页,子页面引用后出现当前上下文中不存在名称“ViewBag”

    View文件夹下缺少Web.config