『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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 蓝牙键盘鼠标连接手提电脑无响应/罗技K380/雷柏鼠标「建议收藏」

    蓝牙键盘鼠标连接手提电脑无响应/罗技K380/雷柏鼠标「建议收藏」最近买了罗技的K380蓝牙键盘,支持连接三种设备,可以通过按键切换。另外也入手了雷柏的一款鼠标,同样也是支持三种设备切换。今天拿到办公室,连接手提的时候,第一次连接显示匹配错误,然后再搜索一直搜索不到了。经过排查以后,发现是蓝牙设备已经配对,但是没有连接成功,在蓝牙选项里的已配对设备里,把设备删除,重新搜索连接就可以了!转载于:https://www.cnblogs.com/miketian/…

    2022年9月13日
    0
  • 如何将ofd文件转成pdf格式?

    如何将ofd文件转成pdf格式?自电子发票改为ofd格式文件后,很多通知、办公文档也逐步开始用ofd替代,但毕竟使用还不算普及,我们也都更习惯PDF格式。那么如何将OFD文件转成PDF呢?对于不熟悉和不知如何打开OFD文件的人来说,最简单的方法当然是直接转成PDF后再打开查看或修改,这里我们需要用到专门的转换工具。不需要安装软件,只需要搜索speedpdf打开这个在线工具页面就能转换,首页就能找到我们需要的OFD转PDF功能。(除此之外还有XPS和CAD这些很难找到转换工具的格式转换哦)转换过程也非常简单,添加需要转换的文档后,点击

    2022年5月27日
    36
  • python安装cv2模块的方法(python中cv2库)

    python如何安装cv2模块  大家在第一次安装cv2模块的时候可能会犯这样的错误,输入pip(3)installcv2命令后,vc2并没有开始安装,而是返回这样一个错误一个错误:Couldnotfindaversionthatsatisfiestherequirementcv2(fromversions:)Nomatchingdistributionf…

    2022年4月14日
    62
  • Web后端基础知识

    Web后端基础知识文章目录前言一、pandas是什么?二、使用步骤1.引入库2.读入数据总结前言提示:以下是本篇文章正文内容,下面案例可供参考一、pandas是什么?示例:pandas是基于NumPy的一种工具,该工具是为了解决数据分析任务而创建的。二、使用步骤1.引入库代码如下(示例):importnumpyasnpimportpandasaspdimportmatplotlib.pyplotaspltimportseabornassnsimportwarnings

    2022年6月16日
    36
  • python 微信自动回复机器人

    python 微信自动回复机器人python微信自动回复机器人导入wxautohttps://github.com/cluic/wxauto#!python3#-*-coding:utf-8-*-“””Author:tikic@qq.comSource:https://github.com/cluic/wxautoLicense:MITLicenseVersion:3.3.5.3″””fromtokenizeimportNamefromunicodedataimportnameim

    2022年10月1日
    0
  • 临界区 互斥量 事件 信号量_互斥信号量与同步信号量

    临界区 互斥量 事件 信号量_互斥信号量与同步信号量四种进程或线程同步互斥的控制方法:1、临界区:通过对多线程的串行化来访问公共资源或一段代码,速度快,适合控制数据访问。2、互斥量:为协调共同对一个共享资源的单独访问而设计的。3、信号量:为控制一个具有有限数量用户资源而设计。4、事 件:用来通知线程有一些事件已发生,从而启动后继任务的开始。临界区(Critical Section)保证在某一时刻只有一个线程能访问数据的简便办法。在任意…

    2022年8月18日
    9

发表回复

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

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