国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

C#Process類的思考

2019-11-14 16:35:16
字體:
來源:轉載
供稿:網友

在這里,我先給自己留個印象

下面我們用C#實現一個調用Dos命令的小程序,讓大家對系統進程能有個直觀的了解.要使用PRocess類,首先要引入System.Diagnostic命名空間,然后定義一個新的Process類,將其制定為打開一個Cmd.exe的命令,然后根據其的StanderInput和StanderOutput對其進行命令的輸入和信息的讀出.具體程序如下:

Process p=new Process();

p.StartInfo.FileName="cmd.exe"; //設置啟動的進程命令

/**設置是否標準輸入輸出和標準錯誤,當這些都設為true時

**UseShellExcute必須為 false*/

p.StartInfo.UseShellExcute=false;

p.StartInfo.RedirectStanderInput=true;  

p.StartInfo.RedirectStanderOutput=true;  

p.StartInfo.RedirectStanderError=true;   

p.StartInfo.CreatNoWindows=true;

p.start();

//向Dos窗口中輸入ping的命令,這里的

//輸入退出窗口的命令

p..StandardInput.WriteLine("Exit");

/**這里用ReadToEnd讀出輸出并將其賦給一個string值,這里要

**注意的是ReadToEnd這個命令是在調用的程序結束后才可以執行的,所以

**要是把這句放在上面的"Exit"之后,程序就會進入一個死循環*/

string output= p.StandardOutput.ReadToEnd();

主要的工作已經完成了,下來就看你怎樣利用程序的輸入輸出來完成一些功能了.

 

在這里我也寫了一個實現:

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Diagnostics;  
  6.   
  7. namespace Consoleapplication1  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Process process = new Process();  
  14.             string strBatPath = "E:/test.bat";  
  15.             string mess = ExecuteBAT(strBatPath, process);  
  16.             Console.WriteLine(mess);  
  17.             Console.ReadKey();  
  18.   
  19.         }  
  20.         private static string ExecuteBAT(string strBatPath, Process pro)  
  21.         //文件路徑;要執行bat文件的進程,返回執行結果  
  22.         {  
  23.             string mess = "";  
  24.   
  25.             try  
  26.             {  
  27.                 pro.StartInfo.UseShellExecute = true;  
  28.                 //strBatPath是bat文件路徑  
  29.                 pro.StartInfo.FileName = strBatPath;  
  30.                 pro.StartInfo.CreateNoWindow = true;  
  31.                 if (pro.Start())  
  32.                 {  
  33.                     //寫日志文件  
  34.                     mess = DateTime.Now.ToLongDateString() + "  " + strBatPath + "執行成功";  
  35.                 }  
  36.                 else  
  37.                 {  
  38.                     mess = string.Format("執行{0}失敗.", strBatPath);  
  39.                 }  
  40.             }  
  41.             catch (Exception ex)  
  42.             {  
  43.                 mess = ex.Message;  
  44.             }  
  45.             finally  
  46.             {  
  47.                 pro.Close();  
  48.             }  
  49.             return mess;  
  50.         }  
  51.   
  52.   
  53.     }  
  54. }  

 現在 在寫一個讀入文件的C#方法

C#代碼  收藏代碼
  1. public static void printFile(string strFileName)  
  2.        {  
  3.            StreamReader srd;  
  4.            try  
  5.            {  
  6.                srd = File.OpenText(strFileName);  
  7.   
  8.            }  
  9.            catch (Exception e)  
  10.            {  
  11.                Console.WriteLine(e.Message);  
  12.                Console.WriteLine("File not read");  
  13.                return;  
  14.            }  
  15.            while(srd.Peek()!=-1)  
  16.            {  
  17.                string str = srd.ReadLine();  
  18.                Console.WriteLine(str);  
  19.            }  
  20.            Console.WriteLine("End of read");  
  21.            srd.Close();  
  22.        }  
  23.        public static void InputFile(string strFileName)  
  24.        {  
  25.            StreamWriter swt;  
  26.            try  
  27.            {  
  28.                swt = File.CreateText(strFileName);  
  29.   
  30.            }  
  31.            catch (Exception e)  
  32.            {  
  33.                Console.WriteLine(e.Message);  
  34.                Console.WriteLine("File not Write");  
  35.                return;  
  36.            }  
  37.            swt.WriteLine("chenhailong");  
  38.            swt.Close();  
  39.   
  40.        }  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 河源市| 灵寿县| 收藏| 云龙县| 大悟县| 晋江市| 凤山市| 咸丰县| 中卫市| 黄浦区| 南木林县| 全州县| 景泰县| 盘山县| 嫩江县| 广灵县| 纳雍县| 岳阳县| 邳州市| 黔东| 巴南区| 霍林郭勒市| 巫溪县| 旬阳县| 桃江县| 信丰县| 谢通门县| 道真| 岳阳县| 嫩江县| 油尖旺区| 广灵县| 万山特区| 民和| 遂川县| 大庆市| 龙里县| 忻州市| 荆州市| 定安县| 许昌县|