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

『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();
            #endregion
            string 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://my.oschina.net/skyler/blog/706086

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/108953.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 350页前端校招面试题直击大厂:前端基础、前端核心、计算机基础、项目、Hr面…「建议收藏」

    350页前端校招面试题直击大厂:前端基础、前端核心、计算机基础、项目、Hr面…「建议收藏」前言考虑到关注的小伙伴们也会有在校生或应届生,要参加校招的同学,一直挺想总结一些关于校招面试题,赶在春招结束前终于写完了,除了写到前端方面的面试题外,项目、职业发展、H面等高频问题也会有,写的很详细,全方面做好准备,为同学们的校招保驾护航!目录1.HTML2.CSS3.前端基础4.前端核心5.前端进阶6.移动端开发7.计算机基础8.算法与数据结构9.设计模式10.项目11.职业发展12.Hr面正文HTML1.浏览器页面有哪三层构成,分别是什么,作用是什么?2.HTML5的

    2022年6月19日
    40
  • 我看项目管理第一回:认识利益相关方,提高思想意识

    我看项目管理第一回:认识利益相关方,提高思想意识

    2022年2月3日
    42
  • 地图的最佳路径分析[通俗易懂]

    地图的最佳路径分析[通俗易懂]Supermap的交通分析Supermap的交通分析对应的是实际地理信息系统中的最佳路径啦,最佳路径在实际的地理信息系统中会用到,而路径分析实际就是在指定的网络上查找一条路径,使其依次经过若干制定的路有点,并使其成本最小,包括距离成本最小的最短路径和时间成本最小的旅行商分析。步骤一、首先打开一个数据库型的工作空间,在交通分析的选线卡中选择拓扑构网再选择构建二维网络。步骤二、点击下面那…

    2022年8月24日
    13
  • linux efi shell,EFI Shell

    linux efi shell,EFI Shell1.2.ItaniumSystems—TheEFIShellBeforeyoustarttoinstallRedHatEnterpriseLinuxonanItanium,youmusthaveabasicunderstandingoftheEFIShell,whatitdoes,andtheinformationitcanpro…

    2022年7月24日
    13
  • java后端知识概述

    java后端知识概述1,java基础知识包括基本语法,集合类框架,以及java语言的特性,jvm等基本知识点,还有一些高级特性,比如反射,注解等等。2,设计模式设计模式是为了可重用代码,让代码更容易被他人理解、保证代码的可靠性的。通常来说,设计模式在系统开发中都是必不可少的。因为这样会简化,降低系统实现过程中要解决的问题。设计模式是软件工程的基石脉络,而模式是在某一背景下某个问题的一种解决方案。常见的设计模式有工厂模式,单例模式,mvc模式等等。而在开发中,所用到的设计模式,往往会根据实际背景去选择某一设计模式。

    2022年7月7日
    47
  • 《哈佛大学公开课:幸福课》学习笔记(3)「建议收藏」

    《哈佛大学公开课:幸福课》学习笔记(3)「建议收藏」关键词/句:Howcanwegetbeyondthat”comfortablynumb”?Howcanwegetbeyondthat”queitdesperation”?focusedonthehealthandcultivatedit,watereditandshedalighttoit,realizedit.optimism,…

    2022年7月18日
    18

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号