asp.net/asp執行cmd實現方法 與代碼

來源:互聯網
上載者:User

先來看一款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;
}
}
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.