在asp.net(c#)下實(shí)現(xiàn)調(diào)用cmd的方法
更新時(shí)間:2012年01月09日 11:20:01 作者:
通常情況下我們會(huì)用到調(diào)用cmd.exe來實(shí)現(xiàn)一些命令,例如 ping ,等等
下面以ping 為例用到命名空間System.Diagnostics;
System.Diagnostics 命名空間 包含了能夠與系統(tǒng)進(jìn)程 事件日志 和性能計(jì)數(shù)器進(jìn)行交互的類 一般用于幫助診斷和調(diào)試應(yīng)用程序 例如 Debug類用于幫組調(diào)試代碼 Process類能夠控制進(jìn)程訪問 Trace類能夠跟蹤代碼的執(zhí)行情況
Process 用于操作本地或者遠(yuǎn)程進(jìn)程打訪問 通過Process 可以在托管環(huán)境下很容易的操作對外部進(jìn)程的啟動(dòng)或者停止 。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace TestEqual
{
class Program
{
static void Main(string[] args)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "iexplore.exe";
myProcess.StartInfo.Arguments = "http://www.baidu.com";
myProcess.Start();
}
}
}
必須設(shè)置相應(yīng)的FileName和Arguments屬性
下面以ping為例
代碼如下:
string hostname = "http://www.baidu.com"; //或者這里是ip等;
Process prc=new Process();
prc.StartInfo.FileName="cmd.exe";
prc.StartInfo.UseShellExecute=false;
prc.StartInfo.RedirectStandardInput = true;
prc.StartInfo.RedirectStandardOutput = true;
prc.StartInfo.RedirectStandardError = true;
prc.StartInfo.CreateNoWindow = false;
prc.Start();
prc.StandardInput.WriteLine("ping " + hostname);
prc.StandardInput.Close();
Response.Write(prc.StandardOutput.ReadToEnd());
這里還可以調(diào)用很多命令自己可以研究下
System.Diagnostics 命名空間 包含了能夠與系統(tǒng)進(jìn)程 事件日志 和性能計(jì)數(shù)器進(jìn)行交互的類 一般用于幫助診斷和調(diào)試應(yīng)用程序 例如 Debug類用于幫組調(diào)試代碼 Process類能夠控制進(jìn)程訪問 Trace類能夠跟蹤代碼的執(zhí)行情況
Process 用于操作本地或者遠(yuǎn)程進(jìn)程打訪問 通過Process 可以在托管環(huán)境下很容易的操作對外部進(jìn)程的啟動(dòng)或者停止 。
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
namespace TestEqual
{
class Program
{
static void Main(string[] args)
{
Process myProcess = new Process();
myProcess.StartInfo.FileName = "iexplore.exe";
myProcess.StartInfo.Arguments = "http://www.baidu.com";
myProcess.Start();
}
}
}
必須設(shè)置相應(yīng)的FileName和Arguments屬性
下面以ping為例
代碼如下:
復(fù)制代碼 代碼如下:
string hostname = "http://www.baidu.com"; //或者這里是ip等;
Process prc=new Process();
prc.StartInfo.FileName="cmd.exe";
prc.StartInfo.UseShellExecute=false;
prc.StartInfo.RedirectStandardInput = true;
prc.StartInfo.RedirectStandardOutput = true;
prc.StartInfo.RedirectStandardError = true;
prc.StartInfo.CreateNoWindow = false;
prc.Start();
prc.StandardInput.WriteLine("ping " + hostname);
prc.StandardInput.Close();
Response.Write(prc.StandardOutput.ReadToEnd());
這里還可以調(diào)用很多命令自己可以研究下
您可能感興趣的文章:
- C# 執(zhí)行CMD命令并接收返回結(jié)果的操作方式
- C# 調(diào)用命令行執(zhí)行Cmd命令的操作
- C# 使用相同權(quán)限調(diào)用 cmd 傳入命令的方法
- C#隱式運(yùn)行CMD命令(隱藏命令窗口)
- C#調(diào)用CMD命令實(shí)例
- c#通過進(jìn)程調(diào)用cmd判斷登錄用戶權(quán)限代碼分享
- C# cmd中修改顯示(顯示進(jìn)度變化效果)的方法
- C#中調(diào)用命令行cmd開啟wifi熱點(diǎn)的實(shí)例代碼
- 通過C#調(diào)用cmd來修改服務(wù)啟動(dòng)類型
- C#中隱式運(yùn)行CMD命令行窗口的方法
- C#程序調(diào)用cmd.exe執(zhí)行命令
相關(guān)文章
Asp.net XMLHTTP封裝類(GET,Post發(fā)送和接收數(shù)據(jù))
XMLHTTP封裝類可以向遠(yuǎn)程發(fā)送URL和參數(shù),接受返回信息(無亂碼)2008-11-11
ASP.Net中英文復(fù)合檢索文本框?qū)崿F(xiàn)思路及代碼
前段時(shí)間,寫一個(gè)用戶部門的管理頁面,需要對后臺獲取的用戶數(shù)據(jù)實(shí)現(xiàn)英漢檢索功能于是就有了下文,編輯的很詳細(xì)圖文并茂呢?感興趣的你可不要錯(cuò)過了哈,或許本文對你有所幫助呢2013-02-02
ASP.NET 站點(diǎn)地圖(sitemap)簡明教程
畢業(yè)設(shè)計(jì)折騰了近一個(gè)月的時(shí)間,也將近完工階段。下個(gè)禮拜六是論文答辯時(shí)間,所以今天晚上就抽空想去弄一下站點(diǎn)地圖。不怕大俠們笑話,我在以前還真沒弄過這些。以前開發(fā)過幾個(gè)項(xiàng)目都是系統(tǒng)類,也就沒怎么涉及了2012-04-04

