在程序中调用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. objdump的使用方法和 symbol table的每列的含义

    一.objdump的用法 objdump命令的man手册 objdump     [-a] [-b bfname|     --target=bfdname] [-C] [--debugging]   ...

  2. MYSQL 配置slave故障

    之前为主从配置,后来分割成2个单实例.现在环境需要,重新配置为主从,之前参数都已配置好,直接启动,如下: mysql> change master to master_host='192.168 ...

  3. 编辑器未包含main类型解决方法

    将文件移到 src 这个 Java Source Folder 下面去,现在在外面的 java 文件不会被当成一个需要编译的类,eclipse 不会编译 Java Source Folder 外面的任 ...

  4. Entity Framework学习笔记(五)----Linq查询(2)---贪婪加载

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们使用了Linq对Entity Framework进行了一个查询,但是通过学习我们却发现了懒加载给我来的性能上的 ...

  5. C++中的虚函数(类的向上转换,和向下转换)

    1.C++中的封装时为了代码的模块化,继承是为了代码的重用,而多态则是为了接口的重用. 2.C++中的多态是用虚函数来实现的. 3.子类对象向父类指针的转换(向上转换)是安全的,隐式的,而父类对象向子 ...

  6. error: format not a string literal and no format arguments [-Werror=format-security]

    You can put this in your Application.mk to disable treating those warnings as errors: APP_CFLAGS += ...

  7. OC特有语法:分类category,给NSString增加方法计算字符串中数字的个数

    1:分类的使用场景:想对一个类,扩充一些功能,而又不改变原来类的模型,也不用继承,这时OC中的特有语法:分类可以做到: 当然分类也是一个类,也需要声明和实现,声明在.h文件中,实现在.m文件中,格式如 ...

  8. android 解决启动页面加载图片空白以及去掉标题栏

    有时候启动页面根据白天晚上来启动时实现加载不同的图片效果,但是加载时会出现短暂的空白,解决方法如下: android:theme="@android:style/Theme.Transluc ...

  9. android禁止ScrollView自动滚动

    当Scrollview嵌套listview,或者子View的内容过多时,当内容加载完成后,ScrollView中内容的长度会发生改变,ScrollView会自动往下滚动,解决办法:在ScollView ...

  10. 2007: [Noi2010]海拔 - BZOJ

    Description YT市是一个规划良好的城市,城市被东西向和南北向的主干道划分为n×n个区域.简单起见,可以将YT市看作一个正方形,每一个区域也可看作一个正方形.从而,YT城市中包括(n+1)× ...