先來看一款asp教程的做法
shell 函數
命名空間:microsoft.visualbasic
模組:interaction
程式集:microsoft visual basic .net 運行庫(位於 microsoft.visualbasic.dll 中)
運行一個可執行程式,並且如果該程式仍然在運行,則返回一個包含該程式的進程 id 的整數。
public function shell( _
byval pathname as string, _
optional byval style as appwinstyle = appwinstyle.minimizedfocus, _
optional byval wait as boolean = false, _
optional byval timeout as integer = -1 _
) as integer
參數
pathname
必選項。字串。要執行的程式名以及任何需要的參數和命令列開關。pathname 還可以包括磁碟機和目錄路徑或檔案夾。
style
可選項。appwinstyle。從 appwinstyle 枚舉中選擇的值,該枚舉與要在其中運行程式的視窗樣式相對應。如果省略 style,則 shell 使用 appwinstyle.minimizedfocus,這將使程式以最小化啟動並具有焦點。
style 參數可以有以下設定之一:
枚舉值 說明
appwinstyle.hide 隱藏視窗並為隱藏的視窗提供焦點。
appwinstyle.normalfocus 為視窗提供焦點,並以最近的大小和位置顯示視窗。
appwinstyle.minimizedfocus 為視窗提供焦點,並以表徵圖的形式顯示視窗。
appwinstyle.maximizedfocus 為視窗提供焦點,並以全屏方式顯示視窗。
appwinstyle.normalnofocus 將視窗設定為最近的大小和位置。當前使用中視窗保持焦點。
appwinstyle.minimizednofocus 以表徵圖的形式顯示視窗。當前使用中視窗保持焦點。
wait
可選項。boolean。指示 shell 函數是否應等待程式完成的值。如果省略 wait,則 shell 使用 false。
timeout
可選項。integer。wait 為 true 時等待完成的毫秒數。如果省略 timeout,則 shell 使用 -1,表示沒有逾時,shell 直到程式完成時才返回。因此,如果省略 timeout 或將它設定為 -1,則 shell 可能永遠不會將控制返回給程式
下面是.net執行cmd實現代碼順手說一哈,我的系統winxp,iis5.1,.netframeworksdk1.1
完整程式cmd.aspx附上
using system;
using system.collections;
using system.configuration;
using system.data;
using system.linq;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.htmlcontrols;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.xml.linq;
using system.diagnostics;
namespace webform
{
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
response.write(execommand("ping http://www.111cn.net "));
}
public string execommand(string commandtext)
{
process p = new process();
p.startinfo.filename = "cmd.exe";
p.startinfo.useshellexecute = false;
p.startinfo.redirectstandardinput = true;
p.startinfo.redirectstandardoutput = true;
p.startinfo.redirectstandarderror = true;
p.startinfo.createnowindow = true;
string stroutput = null;
try
{
p.start();
p.standardinput.writeline(commandtext);
p.standardinput.writeline("exit");
stroutput = p.standardoutput.readtoend();
p.waitforexit();
p.close();
}
catch (exception e)
{
stroutput = e.message;
}
return stroutput;
}
}
}