如果调用外部普通应用程序, 比如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. Python time 的应用

    测试如下: import time print("chenfulin") # 返回格林威治西部的夏令时区 print("time.altzone %d" % t ...

  2. SQLServer 错误: 15404,无法获取有关 Windows NT 组

    右击-属性-所有者改成sa 测试一下   右击 --- 作业开始步骤---执行成功

  3. 机器人排除标准 robot.txt robot exclusion standard

    18.2 什么是机器人排除标准 <搜索引擎优化宝典(第2版)>第18章机器人.蜘蛛和爬虫,本章主要的内容有:什么是机器人.爬虫和蜘蛛:什么是机器人排除标准; Robots 元标签:用XML ...

  4. js jQuery 右键菜单 清屏

    主要用到了oncontextmenu事件,在oncontextmenu事件中使用return false 屏蔽掉原生右键菜单,再使用event获取鼠标的坐标位置,设置自定义菜单的位置. http:// ...

  5. 使用OpenOffice.org将各类文档转为PDF

    http://blog.zhaojie.me/2010/05/convert-document-to-pdf-via-openoffice.html ————————————————————————— ...

  6. I/O模型(同步、非同步、阻塞、非阻塞)总结

    I/O:同步(synchronous).异步(asynchronous).阻塞(blocking).非阻塞(nonblocking) 1.I/O内部机制 出于安全考虑,用户程序(用户态)是没办法直接操 ...

  7. How MapReduce Works

    转自:http://blog.csdn.net/luyee2010/article/details/8624470 一.从Map到Reduce MapReduce其实是分治算法的一种实现,其处理过程亦 ...

  8. vmstat和iostat命令进行Linux性能监控

    这是我们正在进行的Linux命令和性能监控系列的一部分.vmstat和iostat两个命令都适用于所有主要的类unix系统(Linux/unix/FreeBSD/Solaris). 如果vmstat和 ...

  9. 关于在ubuntu下配置AMD显卡驱动的总结

    同样先卸载先前版本 代码: sudo sh /usr/share/ati/fglrx-uninstall.sh 代码: sudo apt-get remove --purge fglrx fglrx_ ...

  10. SAP中方会计凭证打印解决方案

    中方会计凭证由于 编码格式 以及 科目对照关系 是无法直接使用SAP自带的凭证打印功能的,如下为客户开发的一个解决方案,供各位参考 1).需要定制几个Table的结构 zc0000fit0009[科目 ...