Process类调用exe,返回值以及参数空格问题

(方法一)返回值为int

fileName为调用的exe路径,入口参数为para,其中多个参数用空格分开,当D:/DD.exe返回值为int类型时。


Process p = new Process();  

string fileName = @"D:/DD.exe";  

string para ="aa bb";  

ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(fileName, para);  

p.StartInfo = myProcessStartInfo;  

p.Start();  

while (!p.HasExited)  

{  

p.WaitForExit();  

}  

int returnValue = p.ExitCode;  

(方法二)返回值位string

返回值为string时,首先在生成DD.exe时主函数main返回值为void,但在主函数要用Write输出string,通过StandardOutput.ReadToEnd()获取输出流(即输出的字符串)

    string fileName = @"D:/DD.exe";  

    Process p = new Process();  

    p.StartInfo.UseShellExecute = false;  

    p.StartInfo.RedirectStandardOutput = true;  

    p.StartInfo.FileName = fileName;  

    p.StartInfo.CreateNoWindow = true;  

    p.StartInfo.Arguments = "aa bb cc";//参数以空格分隔

    p.Start();  

    p.WaitForExit();  

    string output = p.StandardOutput.ReadToEnd();  

PS:在输入参数 p.StartInfo.Arguments时,由于是以空格分隔,所以,当参数中含有字符串时,会强行分隔。比如我的入口参数需要以下四个参数:“D:\\Demo\\picWorldCup\\worldCup_cy.png”   “Canon MG3600 series Printer XPS” “330” “471”,如果我直接写成如下形式(即直接用空格连接起来)p.StartInfo.Arguments=“D:\\Demo\\picWorldCup\\worldCup_cy.png Canon MG3600 series Printer XPS 330 471显然是不可以的,此时应该通过转义字符\"把属于一个参数的内容包含起来即可,如

p.StartInfo.Arguments = "\"D:\\Demo\\picWorldCup\\worldCup_cy.png\" \"Canon MG3600 series Printer XPS\" 330 471";

原文地址:https://www.cnblogs.com/llstart-new0201/p/9101679.html

时间: 2024-08-14 15:24:36

Process类调用exe,返回值以及参数空格问题的相关文章

定义一个复数(z=x+iy)类Complex,包含: 两个属性:实部x和虚部y 默认构造函数 Complex(),设置x=0,y=0 构造函数:Complex(int i,int j) 显示复数的方法:showComp()将其显示为如: 5+8i或5-8i 的形式。 求两个复数的和的方法:(参数是两个复数类对象,返回值是复数类对象)public Complex addComp(Compl

因标题框有限,题目未显示完整,以下再放一份: 定义一个复数(z=x+iy)类Complex,包含: 两个属性:实部x和虚部y 默认构造函数 Complex(),设置x=0,y=0 构造函数:Complex(int i,int j) 显示复数的方法:showComp()将其显示为如: 5+8i或5-8i 的形式. 求两个复数的和的方法:(参数是两个复数类对象,返回值是复数类对象)public Complex addComp(Complex C1,Complex C2) 求两个复数的差的方法:(参数

【Android】Android中Service类onStartCommand的返回值有关问题(转)

@Override public int onStartCommand(Intent intent, int flags, int startId) { System.out.println("---------->>onStartCommand2"); return super.onStartCommand(intent, flags, startId); } Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象

Android中Service类onStartCommand的返回值问题

Android开发的过程中,每次调用startService(Intent)的时候,都会调用该Service对象的onStartCommand(Intent,int,int)方法,然后在onStartCommand方法中做一些处理.然后我们注意到这个函数有一个int的返回值,这篇文章就是简单地讲讲int返回值的作用. 从Android官方文档中,我们知道onStartCommand有4种返回值: START_STICKY:如果service进程被kill掉,保留service的状态为开始状态,但

SpringMVC中通过@ResponseBody返回对象,Js中调用@ResponseBody返回值,统计剩余评论字数的js,@RequestParam默认值,@PathVariable的用法

1.SpringMVC中通过@ResponseBody返回对象,作为JQuery中的ajax返回值 package com.kuman.cartoon.controller; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.io.filefilter.FalseFileFilter; import org.slf4j.Logger; import org.s

SqlServer 获得存储过程的返回值(参数返回),别玩了output

declare @dingdanid int declare @fanhuizhi int set @dingdanid = 1 exec 检测订单出库资格 @dingdanid ,@fanhuizhi output   (注意别忘了output否则返回值是NULL) select @fanhuizhi create proc dbo.检测订单出库资格 @dingdanID int,     @returnValue int output as -- 输入变量 订单编号 set @returnV

以函数返回值做参数时,函数调用的顺序

环境:vs2013 在下面的代码中 1 //类似于下面的代码 2 3 foo(char*,char*,char*); 4 5 char* str ="A#B#C"; 6 7 foo(strtok(str,"#"),strtok(NULL,"#"),strtok(NULL,"#")); 预计让函数foo得到("A","B","C")的参数,程序编译的时候没问题,但是运行

返回值与参数的泛型一致

今天解决了一个返回值于参数的泛型一致的问题,还不用强制类型转换,呵呵 函数的原型可以这样写 1 public static <T> ClassA<T> build(ClassA<T> classA, String message, String code);

shell调用函数返回值深入分析

编写shell脚本过程中,我们经常会自定义一些函数,并根据函数的返回值不同来执行相应的流程,那么我们如何来获取函数的返回值呢? 首先shell中调用函数有两种方式: 第一种:value=`function_name [arg1 arg2 ......]` 或 第二种:function_name [arg1 arg2 ......] echo $? 这两种有什么区别呢? 举个例子来说: [[email protected] ~]# cat test.sh #!/bin/sh function aa

string类find函数返回值判定

 string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int main() { static const size_t npos = -1; string s = "Alice Bob Charlie"; size_t position; position = s.find("none"); //position = s.find(