日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

『C#基础』调用CMD的一个小工具

發布時間:2023/12/10 C# 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 『C#基础』调用CMD的一个小工具 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

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

界面:

代碼:

主要執行代碼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

總結

以上是生活随笔為你收集整理的『C#基础』调用CMD的一个小工具的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。