如果调用外部普通应用程序, 比如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中调用外部程序及批处理文件的更多相关文章

  1. PowerShell中调用外部程序和进程操作命令例子

    学习PowerShell,我们不指望通过C#编程去搞定所有事情,我们应该记住cmd.exe或者说批处理给我们留下的宝贵财富——通过调用外部程序去解决问题.调用了外部程序,势必就要对进程进行管理,这就是 ...

  2. JAVA中调用外部程序,并等待其退出(涉及Runtime和ProcessBuilder)

    这段时间要写一个java调用外部程序的功能,踩了几个坑,这里分享一下. 首先用的是RunTime,调用代码如下: Process pro = Runtime.getRuntime().exec(&qu ...

  3. Unity3D中调用外接摄像头,并保存为图片文件

    http://bbs.9ria.com/thread-170539-1-1.html 项目要求调用摄像头,并且把图像保存下来,上传到服务器. 这里有几个难点,调用摄像头是很简单的,unity已经提供好 ...

  4. C#使用Process类调用外部程序(转)

    在程序开发中,一个程序经常需要去调用其他的程序,C#中Process类正好提供了这样的功能.它提供对本地和远程进程的访问并使您能够启动和停止本地系统进程.一.启动进程实例 Process myProc ...

  5. start 调用外部程序

    批处理中调用外部程序的命令(该外部程序在新窗口中运行,批处理程序继续往下执行,不理会外部程序的运行状况),如果直接运行外部程序则必须等外部程序完成后才继续执行剩下的指令 例:start explore ...

  6. C#Process调用外部程序

    前言 使用C#调用外部程序,一种是通过Process类,一种是通过命令行,本文主要说一下使用C#中的Process类调用外部程序的方式. 过程: 1. 创建Process对象 2. 配置启动选项(输入 ...

  7. Delphi - 调用外部程序并阻塞到外部程序中

    Delphi 调用外部程序并阻塞到外部程序中 背景说明: 前段时间开发一个数据转换的系统,业务逻辑中说明数据需要压缩成.tar.gz格式. 我在Windows系统下采用,先生成批处理文件,然后调用Wi ...

  8. Unity3D中C#和js方法相互调用

    通过查找资料,Unity3D中C#和js要相互调用彼此的方法,js文件必须放在"Standard Assets". "Pro Standard Assets" ...

  9. Unity3D中关于场景销毁时事件调用顺序的一点记录

    先说一下我遇到的问题,我弄了一个对象池管理多个对象,对象池绑定在一个GameObject上,每个对象在OnBecameInvisible时会进行回收(即移出屏幕就回收),但是当场景切换或停止运行程序时 ...

随机推荐

  1. 【转】 oracle 层次查询判断叶子和根节点

    Oracle 9i判断是叶子或根节点,是比较麻烦的一件事情,SQL演示脚本如下: DROP TABLE idb_hierarchical; create TABLE idb_hierarchical ...

  2. CDH 修改配置注意事项

    cdh 在使用时如果修改了配置文件,需要重启过时服务,而不是重启,重启过时服务才会修改配置文件

  3. <Effective Django>读书笔记

    In Django parlance, a project is the final product, and it assembles one or more applications togeth ...

  4. 工具软件 PYUV打开raw图片

    引自:http://blog.csdn.net/lavenderss/article/details/51495648 [pYUV]如何打开YUV/RGB图片 pYUV工具本身使用起来比较简单,但如果 ...

  5. Knockout开发中文API系列4–监控属性数组

    PS:这个翻译系列好久都没有更新了,实在是不应该,一方面是由于时间不多,另一方面也由于自身惰性太大,从今天起接着更新,会在最近的一月内把这个系列中文API文档翻译完整. 如果你想侦测并响应一个对象的变 ...

  6. 编译JDK源代码【转】

    用Eclipse Debug,当跟踪进jdk api里时(比如javax.swing包里的类),无法查看某些local filed的值.这是因为jdk里的代码在打包时删除了一些用于调试的信息,以减小安 ...

  7. 【C】——如何生成静态库和动态库

    对于一个编译型语言,编译器工作时总要经过预处理.编译.链接等几个过程.以 C/C++ 为例:预处理器(cpp)将每个 .c/.cpp 文件中 #include 的头文件都合并到 .c/.cpp 文件中 ...

  8. ELASTICSEARCH 中暂时移除一个节点

    ELASTICSEARCH 中暂时移除一个节点 版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 前言 在维护ES集群的过程中,我们会经常遇到将某个ES实 ...

  9. sudo开发常用命令总结

    ansible ws -m "shell" -a "echo 'wangsong ALL=(ALL) NOPASSWD: /usr/local/bin/superviso ...

  10. 【Java集合源码剖析】Hashtable源码剖析

    转载出处:http://blog.csdn.net/ns_code/article/details/36191279 Hashtable简介 Hashtable同样是基于哈希表实现的,同样每个元素是一 ...