在Unity3d中调用外部程序及批处理文件
如果调用外部普通应用程序, 比如notepad.exe 这样调用
static public bool ExecuteProgram(string exeFilename, string workDir, string args)
{
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.FileName = exeFilename;
info.WorkingDirectory = workDir;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.Arguments = args;
info.UseShellExecute = false;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; System.Diagnostics.Process task = null; bool rt = true; try
{
task = System.Diagnostics.Process.Start(info);
if (task != null)
{
task.WaitForExit();
}
else
{
return false;
}
}
catch (Exception e)
{
Debug.LogError("Error: " + e.ToString());
return false;
}
finally
{
if (task != null && task.HasExited)
{
string output = task.StandardError.ReadToEnd();
if (output.Length > )
{
Debug.LogError(output);
} output = task.StandardOutput.ReadToEnd();
if (output.Length > )
{
Debug.Log("Error: " + output);
} rt = (task.ExitCode == );
}
} return rt;
}
如果需要调用Window的批处理文件BAT,
或者含有控制台输出的程序,
或者使用上面的方法卡死, 则使用下面的方法运行
static bool ExecuteProgram(string exeFilename, string workDir, string args)
{
System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
info.FileName = exeFilename;
info.WorkingDirectory = workDir;
info.UseShellExecute = true;
info.Arguments = args;
info.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; System.Diagnostics.Process task = null;
bool rt = true;
try
{
Debug.Log("ExecuteProgram:" + args); task = System.Diagnostics.Process.Start(info);
if (task != null)
{
task.WaitForExit();
}
else
{
return false;
}
}
catch (Exception e)
{
Debug.LogError("ExecuteProgram:" + e.ToString());
return false;
}
finally
{
if (task != null && task.HasExited)
{
rt = (task.ExitCode == );
}
} return rt;
}
在Unity3d中调用外部程序及批处理文件的更多相关文章
- PowerShell中调用外部程序和进程操作命令例子
学习PowerShell,我们不指望通过C#编程去搞定所有事情,我们应该记住cmd.exe或者说批处理给我们留下的宝贵财富——通过调用外部程序去解决问题.调用了外部程序,势必就要对进程进行管理,这就是 ...
- JAVA中调用外部程序,并等待其退出(涉及Runtime和ProcessBuilder)
这段时间要写一个java调用外部程序的功能,踩了几个坑,这里分享一下. 首先用的是RunTime,调用代码如下: Process pro = Runtime.getRuntime().exec(&qu ...
- Unity3D中调用外接摄像头,并保存为图片文件
http://bbs.9ria.com/thread-170539-1-1.html 项目要求调用摄像头,并且把图像保存下来,上传到服务器. 这里有几个难点,调用摄像头是很简单的,unity已经提供好 ...
- C#使用Process类调用外部程序(转)
在程序开发中,一个程序经常需要去调用其他的程序,C#中Process类正好提供了这样的功能.它提供对本地和远程进程的访问并使您能够启动和停止本地系统进程.一.启动进程实例 Process myProc ...
- start 调用外部程序
批处理中调用外部程序的命令(该外部程序在新窗口中运行,批处理程序继续往下执行,不理会外部程序的运行状况),如果直接运行外部程序则必须等外部程序完成后才继续执行剩下的指令 例:start explore ...
- C#Process调用外部程序
前言 使用C#调用外部程序,一种是通过Process类,一种是通过命令行,本文主要说一下使用C#中的Process类调用外部程序的方式. 过程: 1. 创建Process对象 2. 配置启动选项(输入 ...
- Delphi - 调用外部程序并阻塞到外部程序中
Delphi 调用外部程序并阻塞到外部程序中 背景说明: 前段时间开发一个数据转换的系统,业务逻辑中说明数据需要压缩成.tar.gz格式. 我在Windows系统下采用,先生成批处理文件,然后调用Wi ...
- Unity3D中C#和js方法相互调用
通过查找资料,Unity3D中C#和js要相互调用彼此的方法,js文件必须放在"Standard Assets". "Pro Standard Assets" ...
- Unity3D中关于场景销毁时事件调用顺序的一点记录
先说一下我遇到的问题,我弄了一个对象池管理多个对象,对象池绑定在一个GameObject上,每个对象在OnBecameInvisible时会进行回收(即移出屏幕就回收),但是当场景切换或停止运行程序时 ...
随机推荐
- Java 1.7 ThreadPoolExecutor源代码解析
相比1.6,1.7有些变化: 1. 添加了一个TIDYING状态.这个状态是介于STOP和TERMINATED之间的.假设运行完terminated钩子函数后状态就变成TERMINATE ...
- flume 多chanel配置
#配置文 a1.sources= r1 a1.sinks= k1 k2 a1.channels= c1 c2 #Describe/configure the source a1.sources.r1. ...
- open source Swift, Objective-C and the next 20 years of development
Q&AApple's Craig Federighi talks open source Swift, Objective-C and the next 20 years of develop ...
- 【Java】定义魔法数字,以及枚举类的构造方法的使用
JavaWeb项目中需要定义各种常量时,常用方法有: 写到Property配置文件中,用静态代码块优先加载配置文件.参考http://www.cnblogs.com/guxin/p/java-prop ...
- 获取linux内核的配置项(包含模块module)_转
转自:提取已有的内核配置文件 由于有时候所做的内核配置文件需要移植到其他的内核源码中,此时又忘了保存,这时以下方法就可以满足你了. 1.首先这两个配置的位于(init/Kconfig): 2. 如果要 ...
- CAS (12) —— CAS TicketRegistry使用JPA方案数据源c3p0与JNDI
CAS (12) -- CAS TicketRegistry使用JPA方案数据源c3p0与JNDI tomcat版本: tomcat-8.0.29 jdk版本: jdk1.8.0_65 cas版本: ...
- JMeter (1) —— JMeter与WebDriver安装与测试(101 Tutorial)
JMeter (1) -- JMeter与WebDriver安装与测试(101 Tutorial) 主要内容 JMeter安装 WebDriver安装 一个简单的JMeter+WebDriver示例 ...
- VC中使用ADO操作数据库的方法 SQL2000
(1).引入ADO类 #import "c:\program files\common files\system\ado\msado15.dll" \ no_namespace \ ...
- jQuery(三):样式操作
一.DOM操作分类 DOM Core:任何一种支持DOM的编程语言都可以使用它,例如:getElementById(). HTML-DOM:用于处理HTML文档,例如:document.forms. ...
- Linux 下安装 mysql
1. Mysql数据库的安装与初始化 1.1 Mysql安装 安装Mysql: $ cd /usr/local/ //查询出已安装的mariadb $ rpm -qa|grep mariad ...