Interaction.Shell 方法
執行可執行程式,並在它仍在執行中時傳回一個整數 (整數中包含此程式的處理序 ID)。
命名空間: Microsoft.VisualBasic
組件: Microsoft.VisualBasic (在 microsoft.visualbasic.dll 中)
語法
'宣告
Public Shared Function Shell ( _
PathName As String, _
<OptionalAttribute> Optional Style As AppWinStyle = AppWinStyle.MinimizedFocus, _
<OptionalAttribute> Optional Wait As Boolean = False, _
<OptionalAttribute> Optional Timeout As Integer = -1 _
) As Integer
'用途
Dim PathName As String
Dim Style As AppWinStyle
Dim Wait As Boolean
Dim Timeout As Integer
Dim returnValue As Integer
returnValue = Interaction.Shell(PathName, Style, Wait, Timeout)
public static int Shell (
string PathName,
[OptionalAttribute] AppWinStyle Style,
[OptionalAttribute] bool Wait,
[OptionalAttribute] int Timeout
)
public:
static int Shell (
String^ PathName,
[OptionalAttribute] AppWinStyle Style,
[OptionalAttribute] bool Wait,
[OptionalAttribute] int Timeout
)
public static int Shell (
String PathName,
/** @attribute OptionalAttribute() */ AppWinStyle Style,
/** @attribute OptionalAttribute() */ boolean Wait,
/** @attribute OptionalAttribute() */ int Timeout
)
public static function Shell (
PathName : String,
Style : AppWinStyle,
Wait : boolean,
Timeout : int
) : int
參數
PathName
必要項。String。要執行的程式名稱,以及任何必要的引數和命令列參數。PathName 也可包含磁碟機和目錄路徑或資料夾。如果您不知道此程式的路徑,可以使用 My.Computer.FileSystem.GetFiles 方法 來尋找。例如,您可以呼叫
My.Computer.FileSystem.GetFiles("C:\", True, "testFile.txt")
,這樣會傳回磁碟機 C:\ 上每個名為testFile.txt
之檔案的完整路徑。
- Style
選擇項。AppWinStyle - 選自 AppWinStyle 列舉型別 的值,可指定此程式執行所在的視窗之樣式。如果省略 Style,則 Shell 會使用 AppWinStyle.MinimizedFocus (其會以最小化狀態啟動程式,並具有焦點)。
- Wait
選擇項。Boolean - 指出 Shell 函式是否應該等候程式完成的值。如果省略 Wait,則 Shell 會使用 False。
- Timeout
選擇項。Integer - 在 Wait 為 True 時,要等候完成的毫秒數。如果省略 Timeout,則 Shell 會使用 -1,這表示沒有逾時,且 Shell 要等到程式完成後才會傳回。因此,如果您省略 Timeout 或是將它設定為 -1,則 Shell 有可能永遠都不會將控制權傳回給您的程式。
傳回值
執行可執行程式,並在它仍在執行中時傳回一個整數 (整數中包含此程式的處理序 ID)。
備註
如需詳細資訊,請參閱 Visual Basic 的主題 Shell 函式。
Shell 函式的傳回值需視在 Shell 傳回時,於 PathName 中具名的程式是否仍在執行而定。如果您將 Wait 設定為 True,且程式在逾時到期之前就結束了,則 Shell 會傳回零。如果逾時已到期,或是您省略 Wait 或將它設定為 False,則 Shell 會傳回此程式的處理序 ID。處理序 ID 為識別執行中程式的唯一數字。
無法啟動
如果 Shell 函式無法啟動具名的程式,則會發生 FileNotFoundException 錯誤。例如,當您嘗試從應用程式透過 System.Windows.Forms 執行 16 位元程式 (例如 command.com
) 時,可能會發生這個情況。解決方法是執行會呼叫所需之 16 位元程式的 32 位元程式。在 command.com
的情況下,您可以改為執行 cmd.exe
。
等候完成
Shell 函式預設會以非同步方式執行程式。這表示,以 Shell 函式啟動的程式可能無法在執行 Shell 函式之後的陳述式之前完成執行。如果您想要等候程式完成之後再繼續執行,請將 Wait 設定為 True。
判斷結束代碼
當處理序結束時,會傳回結束代碼 (Exit Code)。但是,您無法使用 Shell 來擷取此結束代碼,因為 Shell 會在等候結束時傳回零,而且此處理序會在與 Shell 不同的物件中執行。
若要擷取處理序中的結束代碼,則必須撰寫自己的程式碼,以啟始處理序並等待結束。下列範例將示範如何初始化處理序、等候它結束,以及擷取它的結束代碼。
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))
保護檔案規格
您一定要用引號括住整個路徑和檔案規格,如下列範例所示。
ID = Shell("""C:\Program Files\display.exe"" -a -q", , True, 100000)
字串常值內的每一組相鄰的雙引號 (" ") 都會解譯成字串中的一個雙引號字元。因此,之前的範例會將下列字串顯示給 Shell 函式:
"C:\Program Files\display.exe" -a -q
如果您未使用引號括住路徑,Windows 會在 C:\ 目錄中尋找名為 Program.exe
的檔案,而不會在 C:\Program Files 目錄中尋找 display.exe
。
安全性注意事項 |
---|
如果您未使用引號括住路徑和檔案規格,則當檔名或路徑節點有包含空格時,就可能發生安全上的風險。在之前的範例中,路徑節點 |
安全性注意事項 |
---|
Shell 函式需要 Unmanaged 程式碼的使用權限,而該權限在部分信任的情況下,可能會影響其執行。如需詳細資訊,請參閱 SecurityPermission 和 程式碼存取使用權限。 |
範例
下列範例會使用 Shell 函式來執行使用者指定的應用程式。將 AppWinStyle.NormalFocus 指定為第二個引數將會以標準大小開啟應用程式,並為它提供焦點。
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.
平台
Windows 98、 Windows 2000 SP4、 Windows CE、 Windows Millennium Edition、 Windows Mobile for Pocket PC、 Windows Mobile for Smartphone、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition
.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。
版本資訊
.NET Framework
支援版本:2.0、1.1、1.0
.NET Compact Framework
支援版本:2.0
請參閱
參考
Interaction 類別
Interaction 成員
Microsoft.VisualBasic 命名空間
ArgumentException
FileNotFoundException
NullReferenceException
其他資源
Shell 函式
AppWinStyle 列舉型別
My.Computer.FileSystem.GetFiles 方法
AppActivate 函式