『C#基礎』調用CMD的一個小工具

由于經常要使用CMD的一些命令,比如查看IP,Ping一個網址之類的。于是就寫了一個調用CMD.exe的小工具。

主要就是實現這樣一個事情:調用CMD.exe然后傳給它我想要執行的命令,最后獲取結果。

界面:

image

image

代碼:

主要執行代碼using System.Diagnostics;
using System.IO;namespace Client
{class ExcuteCMD{static Process p = new Process();public static string Excute(string cmd){//創建Process對象p.StartInfo.FileName = "cmd.exe";          //要調用的程序 p.StartInfo.UseShellExecute = false;       //關閉Shell的使用 p.StartInfo.RedirectStandardInput = true;  //重定向標準輸入 p.StartInfo.RedirectStandardOutput = true; //重定向標準輸出 p.StartInfo.RedirectStandardError = true;  //重定向錯誤輸出 p.StartInfo.CreateNoWindow = true;         //設置不顯示窗口 p.Start();  //啟動進程 p.StandardInput.WriteLine(cmd); //要執行的命令 p.StandardInput.WriteLine("exit");#region 吸收版權信息p.StandardOutput.ReadLine();p.StandardOutput.ReadLine();p.StandardOutput.ReadLine();p.StandardOutput.ReadLine();p.StandardOutput.ReadLine();#endregionstring strRst = p.StandardOutput.ReadToEnd();  //從輸出流獲取命令執行結果 // logOut(strRst,cmd); // 記錄執行到日志文件return strRst;}public static void closeCMD(){p.Close();}private static void logOut(string log,string cmd){FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Write);StreamWriter sw = new StreamWriter(fs);sw.Flush();sw.BaseStream.Seek(0, SeekOrigin.End);sw.WriteLine(cmd + log);sw.WriteLine();sw.Flush();sw.Close();fs.Close(); }}
}

WPF界面代碼using System.Windows;
using System.Windows.Input;namespace Client
{/// <summary>/// MainWindow.xaml 的交互邏輯/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();tbCmd.Focus();}private void btnSubmit_Click(object sender, RoutedEventArgs e){lblResult.Content = ExcuteCMD.Excute(tbCmd.Text);            }private void btnClose_Click(object sender, RoutedEventArgs e){ExcuteCMD.closeCMD();this.Close();}private void btnPingQQ_Click(object sender, RoutedEventArgs e){lblResult.Content = ExcuteCMD.Excute("Ping www.qq.com");}private void btnIPConfig_Click(object sender, RoutedEventArgs e){lblResult.Content = ExcuteCMD.Excute("ipconfig");}private void tbCmd_KeyDown(object sender, KeyEventArgs e){if (e.Key == Key.Enter){btnSubmit_Click(sender, e);}}}
}

WPF界面代碼<Window x:Class="Client.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="CMD命令執行工具" Height="300" Width="478" MinWidth="400" MinHeight="300" Icon="/Client;component/Images/21.ico"><Grid><Grid.RowDefinitions><RowDefinition Height="210*" /><RowDefinition Height="28*" /><RowDefinition Height="23*" /></Grid.RowDefinitions><Button Content="執行" Height="23" Margin="0,0,66,5" Name="btnSubmit" VerticalAlignment="Bottom" TabIndex="2" Click="btnSubmit_Click" HorizontalAlignment="Right" Width="60" Grid.Row="1" /><TextBox Height="23" Name="tbCmd" VerticalAlignment="Bottom" Margin="0,0,132,5" TabIndex="1" Grid.Row="1" KeyDown="tbCmd_KeyDown" /><Button Content="結束" Height="23" HorizontalAlignment="Right" Margin="0,0,0,5" Name="btnClose" VerticalAlignment="Bottom" Width="60" Click="btnClose_Click" Grid.Row="1" /><ScrollViewer HorizontalAlignment="Stretch" Name="scrollViewer1" VerticalAlignment="Stretch"><Label Height="Auto" Name="lblResult" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" /></ScrollViewer><Button Content="PingQQ" Height="23" HorizontalAlignment="Left" Name="btnPingQQ" VerticalAlignment="Top" Width="56" Click="btnPingQQ_Click" Grid.Row="2" /><Button Content="IPConfig" Height="23" HorizontalAlignment="Left" Margin="62,0,0,0" Name="btnIPConfig" VerticalAlignment="Top" Width="56" Click="btnIPConfig_Click" Grid.Row="2" /></Grid>
</Window>

轉載于:https://www.cnblogs.com/sitemanager/archive/2012/03/05/2380551.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274966.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274966.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274966.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

小姐姐:如何參與大型開源項目-Taro 共建

大家好&#xff0c;我是若川。持續組織了5個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。“本文來自前端程序…

JavaWeb學習總結(十七)——JSP中的九個內置對象

2019獨角獸企業重金招聘Python工程師標準>>> 一、JSP運行原理 每個JSP 頁面在第一次被訪問時&#xff0c;WEB容器都會把請求交給JSP引擎&#xff08;即一個Java程序&#xff09;去處理。JSP引擎先將JSP翻譯成一個_jspServlet(實質上也是一個servlet) &#xff0c;然…

C#網絡編程(異步傳輸字符串) - Part.3[轉自JimmyZhang博客]

源碼下載&#xff1a;http://www.tracefact.net/SourceCode/Network-Part3.rar C#網絡編程(異步傳輸字符串) - Part.3 這篇文章我們將前進一大步&#xff0c;使用異步的方式來對服務端編程&#xff0c;以使它成為一個真正意義上的服務器&#xff1a;可以為多個客戶端的多次請求…

chrome黑暗模式_黑暗模式:如何克服黑暗面

chrome黑暗模式This article has been written by Redmadrobot Design Lab. Translated and reposted with permission by Alconost Inc., professional translation and localization company.本文由 Redmadrobot設計實驗室 撰寫 。 經過 專業翻譯和本地化公司 Alconost Inc.的…

Deco 智能代碼體驗版正式上線啦,快來體驗設計稿一鍵生成代碼~

Deco 是什么&#xff1f;—Deco 智能代碼項目是我們團隊在「前端智能化」方向上的探索&#xff0c;其聚焦設計稿一鍵生成多端代碼這一切入點&#xff0c;實現將 Sketch/Photoshop 等設計稿進行解析并直接生成多端代碼&#xff08;Taro/React/Vue&#xff09;的能力。Deco 可以使…

jQuery 五角星評分

五角星打分 我用的是搜狗輸入法上帶的特殊符號打出來的 空五角星&#xff1a;☆ 實五角星&#xff1a;★ 1.html 1 <ul class"comment"> 2 <li>☆</li> 3 <li>☆</li> 4 <li>☆</li> 5 …

平面設計和網頁設計的規則_從平面設計到用戶界面:這是您應該知道的最重要的規則

平面設計和網頁設計的規則Maybe you’re here because you think UI Design is the future of Graphic Design. Maybe what motivates you is the money. Or maybe you just woke up one day and someone at work told you “So, you are a designer, right? Well, we need an…

即將到來的 ECMAScript 2022 新特性

大家好&#xff0c;我是若川。持續組織了5個月源碼共讀活動&#xff0c;感興趣的可以點此加我微信 ruochuan12 參與&#xff0c;每周大家一起學習200行左右的源碼&#xff0c;共同進步。同時極力推薦訂閱我寫的《學習源碼整體架構系列》 包含20余篇源碼文章。ECMAScript 規范每…

代碼備忘錄

1.用指針&#xff0c;函數調用實現交換兩個變量值 #include<stdio.h> int exchange(int *a,int *b){ int temp; temp*a; *a*b; *btemp;return 0;} int main(){ int i9,j2; int *p1,*p2; p1&i; p2&j; exchange(p1,p2); printf("p1%d,p2%d\n",*p1,*p2);…

mysql實戰38 | 都說InnoDB好,那還要不要使用Memory引擎?

我在上一篇文章末尾留給你的問題是&#xff1a;兩個 group by 語句都用了 order by null&#xff0c;為什么使用內存臨時表得到的語句結果里&#xff0c;0 這個值在最后一行&#xff1b;而使用磁盤臨時表得到的結果里&#xff0c;0 這個值在第一行&#xff1f;今天我們就來看看…

設計類的五個原則_內容設計的5個原則

設計類的五個原則重點 (Top highlight)There are many heuristics and principles for creating good content. Some are created from a UX perspective, others from a content marketing point of view. They range from very long to very concise ones. I reviewed a larg…

Umi 4 RC 發布

大家好&#xff0c;我是若川。感謝大家一年以來的支持和陪伴。這一年疫情反復&#xff0c;年底應該有由于疫情不能回家的小伙伴。在這里先祝福大家&#xff0c;新年快樂。本打算今天不發文&#xff0c;但看到這篇覺得不錯&#xff0c;就發一下。大家好&#xff0c;Umi 4 經過幾…

讓你沉迷的五種設計

讓你沉迷的五種設計 好游戲總是能令人沉迷其中無法自拔&#xff0c;外媒cracked經過分析&#xff0c;發現有五種設計方法必不可少&#xff1b; 1.斯金納箱原理&#xff1a;這是行為心理學派在實驗室內研究動物學習能力的箱形實驗裝置&#xff0c;游戲開發也得益于此&#xff0c…

Java學習路線詳解

有很多的[Java請添加鏈接描述](http://www.hfxms.com.cn/java/)程序員&#xff0c;在初期學習時&#xff0c;通常會對如何學習而感到迷茫。[合肥學碼思請添加鏈接描述](http://www.hfxms.com.cn/)小編就為大家分析如何學好Java編程&#xff0c;相信能幫助那些正在處于迷茫狀態的…

figma下載_在Figma中將約束與布局網格一起使用

figma下載While doing research for the book “Designing in Figma”, I discovered a powerful way to lay out objects using a combination of Layout Grid and Constraints. The interface of Figma does not indicate a connection between the two, so it can be discov…

換一種方式表達

http://player.youku.com/player.php/sid/XMjY2MTE5NDU2/v.swf 轉載于:https://www.cnblogs.com/JCSU/archive/2012/03/17/2403324.html

新的一年,碎片化學習前端,我推薦這幾個公眾號~

大家好&#xff0c;我是若川。假期余額不足&#xff0c;無法充值。快樂的時光總是短暫的。馬上又開始一年的學習和“奮斗”。前端技術日新月異&#xff0c;發展迅速&#xff0c;作為一個與時俱進的前端工程師&#xff0c;需要不斷的學習。這里強烈推薦幾個前端開發工程師必備的…

Java單元測試之JUnit4詳解

2019獨角獸企業重金招聘Python工程師標準>>> Java單元測試之JUnit4詳解 與JUnit3不同&#xff0c;JUnit4通過注解的方式來識別測試方法。目前支持的主要注解有&#xff1a; BeforeClass 全局只會執行一次&#xff0c;而且是第一個運行Before 在測試方法運行之前運行…

我在黑暗中看到你眼中的月光_你好黑暗,我的老朋友

我在黑暗中看到你眼中的月光(Originally published on https://web.dev/prefers-color-scheme/.)(最初發布于https://web.dev/prefers-color-scheme/ 。) 介紹 (Introduction) &#x1f4da; I have done a lot of background research on the history and theory of dark mod…

ant 實現批量打包android應用

很多的應用中需要加上應用推廣的統計&#xff0c;如果一個一個的去生成不同渠道包的應用&#xff0c;效率低不說&#xff0c;還有可能不小心弄錯了分發渠道&#xff0c;使用ant可以批量生成應用。一、添加渠道包信息為了統計渠道信息&#xff0c;就不得不在程序的某個地方加入渠…