資源簡介
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個文件信息
- 上一篇:C# Socket通訊DEMO
- 下一篇:C#網絡爬蟲
評論
共有 條評論