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;
}
C# 调用命令行,参数有空格的更多相关文章
- c#调用命令行遇到带空格的路径
想用 c#调用如下的DOS命令: C:\Program Files\Common Files\System\DBWatcherInstall\dtexec.exe /f C:\Program File ...
- Java:命令行参数的传入和调用
1.传入 传入时机:执行时 格式: //编译 javac Main.java //执行并传入命令行参数 -version java Main -version 此时,参数-version就以Strin ...
- getopt函数的使用——分析命令行参数
getopt(分析命令行参数) getopt(分析命令行参数) 短参数的定义 返回值 范例 getopt_long 相关函数表头文件#include<unistd.h> 函数声明int g ...
- mysql命令行参数(转)
MySQL命令行参数 Usage: mysql [OPTIONS] [database] //命令方式 -?, --help //显示帮助信息并退出 -I, --help //显示帮助信息并退出 ...
- mysql 命令行参数
MySQL命令行参数 Usage: mysql [OPTIONS] [database] //命令方式 例如: mysql -h${HOSTNAME} -P${PORT} -u${USERNAM ...
- 命令行参数(argc, argv)
每个C语言程序都必须有一个称为main()的函数,作为程序启动的起点.当执行程序时,命令行参数(command-line argument)(由shell逐一解析)通过两个入参提供给main()函数. ...
- Python 的命令行参数处理 optparse->argparse
optaprse自2.7版开始弃用:弃用optparse模块,不会进一步开发,将继续开发argparse模块作为替代. 但是用习惯了optparse,还是很好用的撒. optparse使用起来,相比旧 ...
- 【转】getopt分析命令行参数
(一) 在Linux中,用命令行执行可执行文件时可能会涉及到给其加入不同的参数的问题,例如: ./a.out -a1234 -b432 -c -d 程序会根据读取的参数执行相应的操作,在C语言中,这个 ...
- getopt(分析命令行参数)
ref:http://vopit.blog.51cto.com/2400931/440453 相关函数表头文件 #include<unistd.h>定义函数 ...
随机推荐
- Python-Day7 面向对象进阶/异常处理/Socket
一.面向对象高级语法部分 1.静态方法 通过@staticmethod装饰器即可把其装饰的方法变为一个静态方法,什么是静态方法呢?其实不难理解,普通的方法,可以在实例化后直接调用,并且在方法里 ...
- mysql数据库本地化操作
<?php if(!defined('SITE_PATH')){ define('SITE_PATH',dirname(dirname(__FILE__))); } $dbconfig=incl ...
- 仿知乎/途家导航栏渐变文字动画效果-b
demo.gif 效果图如上,简单分析下 1.导航栏一开始是隐藏的,随着scrollView滚动而渐变 2.导航栏左右两边的navigationItem是一直显示的 3.导航栏参考了途家app,使用了 ...
- svn:Repository UUID 'XXX' doesn't match expected UUID 'YYY'
About a month ago, CodePlex have upgraded their TFS servers to to TFS 2010. While this transition wa ...
- WPF解析PDF为图片
偶遇需要解析PDF文件为单张图,此做, http://git.oschina.net/jiailiuyan/OfficeDecoder using System; using System.Colle ...
- 【转】edittext设置点击链接
I put some ClickableSpan in EditText, unfortunately, that spans are still not clickable. When I clic ...
- java集合类(六)About Queue
接上篇“java集合类(五)About Map” 终于来到了java集合类的尾声,太兴奋了,不是因为可以休息一阵了,而是因为又到了开启新知识的时刻,大家一起加油打气!!Come on...Fighti ...
- (C#)的命名规范
http://gray.iteye.com/blog/644626 http://developer.51cto.com/art/200908/143787.htm http://blog.csdn. ...
- github 中删除/更名版本库(repository)
问题描述: github 中版本库创建/删除/更该名称 问题解决: (1)创建版本库(Repository) 注: 在上图中的+按钮图标指示的是创建版本库的按钮 注 ...
- Java多线程——<四>让线程有返回值
一.概述 到目前为止,我们已经能够声明并使一个线程任务运行起来了.但是遇到一个问题:现在定义的任务都没有任何返回值,那么加入我们希望一个任务运行结束后告诉我一个结果,该结果表名任务执行成功或失败,此时 ...