函数参考


Run

运行外部程序.

Run ( "程序" [, "工作目录" [, 显示标志[, 可选标志]]] )

参数

程序 程序所在的完整路径(文件格式为 EXE,BAT,COM 或 PIF).
工作目录 [可选参数] 工作目录.这个路径不一定指向程序所在路径.
显示标志 [可选参数] 启动程序时的初始状态:
  @SW_HIDE = 隐藏窗口
  @SW_MINIMIZE = 最小化窗口
  @SW_MAXIMIZE = 最大化窗口
可选标志 [可选参数] 控制不同选项处理父进程与子进程交互.
  0x1 ($STDIN_CHILD) = 提供一个句柄到子进程的 STDIN 流.
  0x2 ($STDOUT_CHILD) = 提供一个句柄到子进程的 STDOUT 流.
  0x4 ($STDERR_CHILD) = 提供一个句柄到子进程的 STDERR 流.
  0x8 ($STDERR_MERGED) = Provides the same handle for STDOUT and STDERR. Implies both $STDOUT_CHILD and $STDERR_CHILD.
  0x10 ($STDIO_INHERIT_PARENT) = Provide the child with the parent's STDIO streams. This flag can not be combined with any other STDIO flag. This flag is only useful when the parent is compiled as a Console application.
  0x10000 ($RUN_CREATE_NEW_CONSOLE) = The child console process should be created with it's own window instead of using the parent's window. This flag is only useful when the parent is compiled as a Console application.

返回值

成功: 返回所运行程序的 PID(进程标识符).
失败: 返回 0并设置 @error 为非0值.

注意/说明

如果路径中含有空格,请在两侧添加英文引号(").

要运行 DOS(控制台)命令,请使用 Run(@ComSpec & " /c " & 'commandName', "", @SW_HIDE) ;不要忘了 "/c" 前面的 " "

在运行指定程序后脚本将(立即)继续执行后面的语句.若要在指定程序执行完毕之前暂停脚本的执行则请使用 RunWait 函数代替.

Providing the Standard I/O parameter with the proper values permits interaction with the child process through the StderrRead, StdinWrite and StdoutRead functions. Combine the flag values (or use $STDERR_CHILD, $STDIN_CHILD & $STDOUT_CHILD, defined in Constants.au3) to manage more than one stream.

In order for the streams to close, the following conditions must be met: 1) The child process has closed it's end of the stream (this happens when the child closes). 2) AutoIt must read any captured streams until there is no more data. 3) If STDIN is provided for the child, StdinWrite() must be called to close the stream. Once all streams are detected as no longer needed, all internal resources will automatically be freed.
StdioClose 可以用于强制关闭 STDIO 流.

相关

RunWait, RunAs, RunAsWait, ShellExecute, ShellExecuteWait, StderrRead, StdinWrite, StdoutRead, StdioClose, ProcessClose, ConsoleRead

示例/演示


Example()

Func Example()
    ; Run Notepad with the window maximized.
    Local $iPID = Run("notepad.exe", "", @SW_SHOWMAXIMIZED)

    ; Wait 10 seconds for the Notepad window to appear.
    WinWait("[CLASS:Notepad]", "", 10)

    ; Wait for 2 seconds.
    Sleep(2000)

    ; Close the Notepad process using the PID returned by Run.
    ProcessClose($iPID)
EndFunc   ;==>Example