• 大小: 35KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-05-25
  • 语言: C#
  • 标签: C#,CMD  

资源简介

C#调用CMD命令学习,动态读取CMD信息,并动态显示信息,对于执行大文件有用,可以控制进度等。

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private delegate void ProgressInfo(string msg);

        public Form1()
        {
            InitializeComponent();
        }
        private void btnOK_Click(object sender EventArgs e)
        {
            txtInfo.Text = ““;
            txtInfo.Selectionstart = 0;
            StartRun(txtCmd.Text);
        }
        private void StartRun(string cmd)
        {
            Process progress = new Process();
            progress.StartInfo.CreateNoWindow = true;
            progress.StartInfo.FileName = “cmd.exe“;
            progress.StartInfo.UseShellExecute = false;
            progress.StartInfo.RedirectStandardError = true;
            progress.OutputDataReceived += new DataReceivedEventHandler(progress_OutputDataReceived);
            progress.StartInfo.RedirectStandardInput = true;
            progress.StartInfo.RedirectStandardOutput = true;
            progress.Start();
            progress.StandardInput.WriteLine(cmd);
            progress.BeginOutputReadLine();
            progress.StandardInput.WriteLine(“exit“);
            progress.Close();
        }

        void progress_OutputDataReceived(object sender DataReceivedEventArgs e)
        {
            if (e.Data != null)
                this.Invoke(new ProgressInfo(progress) e.Data+“\r\n“);
        }
        private void progress(string msg)
        {
            txtInfo.Text += msg;
            txtInfo.Selectionstart = txtInfo.Text.Length;
            txtInfo.ScrollToCaret();
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件      10752  2014-01-15 18:35  WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe

     文件      26112  2014-01-15 18:35  WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.pdb

     文件      14328  2014-01-15 18:35  WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.vshost.exe

     文件       1843  2014-01-15 18:35  WindowsFormsApplication1\Form1.cs

     文件       3143  2014-01-15 18:35  WindowsFormsApplication1\Form1.Designer.cs

     文件       5814  2014-01-15 18:35  WindowsFormsApplication1\Form1.resx

     文件       1139  2014-01-15 18:35  WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csproj.FileListAbsolute.txt

     文件        847  2014-01-15 18:35  WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.csproj.GenerateResource.Cache

     文件      10752  2014-01-15 18:35  WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.exe

     文件        180  2014-01-15 18:35  WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.Form1.resources

     文件      26112  2014-01-15 18:35  WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.pdb

     文件        180  2014-01-15 18:29  WindowsFormsApplication1\obj\Debug\WindowsFormsApplication1.Properties.Resources.resources

     文件        505  2014-01-15 18:20  WindowsFormsApplication1\Program.cs

     文件       1398  2014-01-15 18:20  WindowsFormsApplication1\Properties\AssemblyInfo.cs

     文件       2898  2014-01-15 18:20  WindowsFormsApplication1\Properties\Resources.Designer.cs

     文件       5612  2014-01-15 18:20  WindowsFormsApplication1\Properties\Resources.resx

     文件       1109  2014-01-15 18:20  WindowsFormsApplication1\Properties\Settings.Designer.cs

     文件        249  2014-01-15 18:20  WindowsFormsApplication1\Properties\Settings.settings

     文件       3759  2014-01-15 18:29  WindowsFormsApplication1\WindowsFormsApplication1.csproj

     文件        962  2014-01-15 18:20  WindowsFormsApplication1.sln

     目录          0  2014-01-15 18:24  WindowsFormsApplication1\obj\Debug\Refactor

     目录          0  2014-01-15 18:20  WindowsFormsApplication1\obj\Debug\TempPE

     目录          0  2014-01-15 18:29  WindowsFormsApplication1\bin\Debug

     目录          0  2014-01-15 18:35  WindowsFormsApplication1\obj\Debug

     目录          0  2014-01-15 18:26  WindowsFormsApplication1\bin

     目录          0  2014-01-15 18:20  WindowsFormsApplication1\obj

     目录          0  2014-01-15 18:20  WindowsFormsApplication1\Properties

     目录          0  2014-01-15 18:35  WindowsFormsApplication1

----------- ---------  ---------- -----  ----

               117694                    28

............此处省略1个文件信息

评论

共有 条评论

相关资源