Interaction.Shell(String, AppWinStyle, Boolean, Int32) 方法

定义

运行一个可执行程序,并且如果该程序仍在运行则返回一个包含该程序的进程 ID 的整数。Runs an executable program and returns an integer containing the program's process ID if it is still running.

public static int Shell (string PathName, Microsoft.VisualBasic.AppWinStyle Style = Microsoft.VisualBasic.AppWinStyle.MinimizedFocus, bool Wait = false, int Timeout = -1);
static member Shell : string * Microsoft.VisualBasic.AppWinStyle * bool * int -> int
Public Function Shell (PathName As String, Optional Style As AppWinStyle = Microsoft.VisualBasic.AppWinStyle.MinimizedFocus, Optional Wait As Boolean = false, Optional Timeout As Integer = -1) As Integer

参数

Pathname
String

必需。Required. StringString. 要执行的程序的名称,以及所有必需的参数和命令行开关。Name of the program to execute, together with any required arguments and command-line switches. PathName 也可以包含驱动器和目录路径或文件夹。PathName can also include the drive and the directory path or folder. 如果不知道程序的路径,则可以使用 GetFiles 进行定位。If you do not know the path to the program, you can use the GetFiles to locate it. 例如,你可以调用 My.Computer.FileSystem.GetFiles("C:", True, "testFile.txt"),这将返回驱动器 C:\ 上任意位置每个名为 testFile.txt 的文件的完整路径。For example, you can call My.Computer.FileSystem.GetFiles("C:", True, "testFile.txt"), which returns the full path of every file named testFile.txt anywhere on drive C:\.

Style
AppWinStyle

可选。Optional. AppWinStyleAppWinStyle. AppWinStyle 中选择的一个值,该值指定将要在其中运行程序的窗口的样式。A value chosen from the AppWinStyle specifying the style of the window in which the program is to run. 如果省略 StyleShell 将使用 AppWinStyle.MinimizedFocus 以最小化方式启动该程序并获得焦点。If Style is omitted, Shell uses AppWinStyle.MinimizedFocus, which starts the program minimized and with focus.

Wait
Boolean

可选。Optional. BooleanBoolean. 一个指示 Shell 函数是否应等待程序完成的值。A value indicating whether the Shell function should wait for completion of the program. 如果省略 Wait,则 Shell 将使用 FalseIf Wait is omitted, Shell uses False.

Timeout
Int32

可选。Optional. IntegerInteger. 如果 WaitTrue,则为等待完成的毫秒数。The number of milliseconds to wait for completion if Wait is True. 如果省略 Timeout,则 Shell 使用 -1,表示没有超时设置并且 Shell 到程序结束时才返回。If Timeout is omitted, Shell uses -1, which means there is no timeout and Shell does not return until the program finishes. 因此,如果省略 Timeout 或将其设置为 -1,则 Shell 可能永远不会将控制返回给您的程序。Therefore, if you omit Timeout or set it to -1, it is possible that Shell might never return control to your program.

返回

一个整数,包含该程序仍在运行时的进程 ID。An integer containing the program's process ID if it is still running. 如果该程序已完成执行,则为 0。0 if the program already finished executing.

异常

Style 不在 0 到 9(包含 0 和 9)范围内。Style is not within range 0 through 9, inclusive.

Shell 找不到 PathName 文件。Shell cannot find the PathName file.

PathNameNothingPathName is Nothing.

示例

下面的示例使用 Shell 函数运行用户指定的应用程序。The following example uses the Shell function to run an application specified by the user. 指定 AppWinStyle.NormalFocus 作为第二个参数将以正常大小打开应用程序,并为其提供焦点。Specifying AppWinStyle.NormalFocus as the second argument opens the application in normal size and gives it the focus.

Dim procID As Integer
' Run calculator.
procID = Shell("C:\Windows\system32\calc.exe", AppWinStyle.NormalFocus)
' The preceding path is for Windows XP.
' The Windows 2000 path is C:\WINNT\system32\calc.exe.

注解

@No__t-0 函数的返回值取决于在 Shell 返回时是否仍在执行 @no__t 中指定的程序。The return value of the Shell function depends on whether the program named in PathName is still executing when Shell returns. 如果将 Wait 设置为 True,并且程序在超时过期之前完成,则 Shell 返回零。If you set Wait to True and the program finishes before the timeout expires, Shell returns zero. 如果超时时间已到,或者省略 @no__t 0 或将其设置为 FalseShell 返回程序的进程 ID。If the timeout expires, or if you omit Wait or set it to False, Shell returns the process ID of the program. 进程 ID 是一个唯一的编号,用于标识正在运行的程序。The process ID is a unique number that identifies the running program.

未能启动Failure to Start

如果 Shell 函数无法启动命名程序,则会发生 FileNotFoundException 错误。If the Shell function cannot start the named program, a FileNotFoundException error occurs. 例如,当你尝试从使用 System.Windows.Forms 的应用程序运行16位程序(例如 command.com)时,可能会发生这种情况。This can happen, for example, when you attempt to run a 16-bit program, such as command.com, from an application using System.Windows.Forms. 为解决此问题,你可以运行一个调用所需的16位程序的32位程序。For a workaround, you can run a 32-bit program that calls the desired 16-bit program. command.com 的情况下,你可以将 @no__t 作为替代方法来运行。In the case of command.com, you can run cmd.exe as an alternative.

等待完成Waiting for Completion

默认情况下,Shell 函数以异步方式运行程序。By default, the Shell function runs the program asynchronously. 这意味着在执行 Shell 函数之后的语句之前,使用 Shell 函数启动的程序可能无法完成执行。This means that a program started with the Shell function might not finish executing before the statements following the Shell function are executed. 如果希望在继续操作之前等待程序完成,请将 Wait 设置为 TrueIf you want to wait for the program to finish before you continue, set Wait to True.

确定退出代码Determining the Exit Code

进程在终止时可以返回退出代码A process can return an exit code when it terminates. 但是,你不能使用 Shell 来检索此退出代码,因为 @no__t 如果在等待终止的情况下返回零,还会在 Shell 的不同对象中运行该进程。However, you cannot use Shell to retrieve this exit code, because Shell returns zero if it waits for termination, and also because the process runs in a different object from Shell.

若要从进程中检索退出代码,你必须编写自己的代码来启动进程并等待终止。To retrieve the exit code from a process, you must write your own code to initiate the process and wait for termination. 下面的示例演示如何启动进程,等待它终止,并检索其退出代码。The following example shows how to initiate a process, wait for it to terminate, and retrieve its exit code.

Dim procID As Integer  
Dim newProc As Diagnostics.Process  
newProc = Diagnostics.Process.Start("C:\WINDOWS\NOTEPAD.EXE")  
procID = newProc.Id  
newProc.WaitForExit()  
Dim procEC As Integer = -1  
If newProc.HasExited Then  
    procEC = newProc.ExitCode  
End If  
MsgBox("Process with ID " & CStr(ProcID) & _  
    " terminated with exit code " & CStr(procEC))  

保护文件规范Protecting the File Specification

应始终将整个路径和文件规范括在引号中,如下面的示例所示。You should always enclose the entire path and file specification in quotation marks, as the following example shows.

ID = Shell("""C:\Program Files\display.exe"" -a -q", , True, 100000)  

字符串文本中的每对相邻双引号(" ")被解释为字符串中的一个双引号字符。Each pair of adjacent double quotation marks (" ") within the string literal is interpreted as one double quotation character in the string. 因此,前面的示例向 Shell 函数提供以下字符串:Therefore, the preceding example presents the following string to the Shell function:

"C:\Program Files\display.exe" -a -q  

如果没有用引号引起来的路径,Windows 将在 C:\ 中查找名为 Program.exe 的文件目录,而不是在 C:\Program Files 目录中 display.exeIf you did not have the path enclosed in quotation marks, Windows would look for a file called Program.exe in the C:\ directory, instead of display.exe in the C:\Program Files directory.

重要

如果未将路径和文件规范括在引号中,则如果文件名或路径节点包含空格,则会产生安全风险。If you do not enclose the path and file specification in quotation marks, there is a security risk if the file name or a path node contains spaces. 在前面的示例中,路径节点 \Program Files 包括空格。In the preceding example, the path node \Program Files includes a space. 如果规范不在引号内并且已在 C: \ 中安装了名为 Program.exe 的程序,例如,违法篡改,则 Windows 将执行它,而不是 display.exeIf the specification were not inside quotation marks and a program named Program.exe had been installed in C:\, for example by illicit tampering, Windows would execute it instead of display.exe.

重要

@No__t-0 函数需要非托管代码权限,这可能会影响在部分信任情况下的执行。The Shell function requires unmanaged code permission, which might affect its execution in partial-trust situations. 有关详细信息,请参阅 SecurityPermission代码访问权限For more information, see SecurityPermission and Code Access Permissions.

适用于

另请参阅