在这里,我先给自己留个印象

下面我们用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的命令,这里的IP值请自己设置

p.StandardInput.WriteLine("ping -n 1 "+IP);

//输入退出窗口的命令

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. namespace ConsoleApplication1
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Process process = new Process();
  13. string strBatPath = "E:/test.bat";
  14. string mess = ExecuteBAT(strBatPath, process);
  15. Console.WriteLine(mess);
  16. Console.ReadKey();
  17. }
  18. private static string ExecuteBAT(string strBatPath, Process pro)
  19. //文件路径;要执行bat文件的进程,返回执行结果
  20. {
  21. string mess = "";
  22. try
  23. {
  24. pro.StartInfo.UseShellExecute = true;
  25. //strBatPath是bat文件路径
  26. pro.StartInfo.FileName = strBatPath;
  27. pro.StartInfo.CreateNoWindow = true;
  28. if (pro.Start())
  29. {
  30. //写日志文件
  31. mess = DateTime.Now.ToLongDateString() + "  " + strBatPath + "执行成功";
  32. }
  33. else
  34. {
  35. mess = string.Format("执行{0}失败.", strBatPath);
  36. }
  37. }
  38. catch (Exception ex)
  39. {
  40. mess = ex.Message;
  41. }
  42. finally
  43. {
  44. pro.Close();
  45. }
  46. return mess;
  47. }
  48. }
  49. }

现在 在写一个读入文件的C#方法

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

C# Process 类的思考的更多相关文章

  1. 使用Process类重定向输出与错误时遇到的问题 (转)

    程序中要调用外部程序cmd.exe执行一些命令行,并取得屏幕输出,使用了Process类,基本代码如下: Process process = new Process(); process.StartI ...

  2. C#的Process类调用第三方插件实现PDF文件转SWF文件

    在项目开发过程中,有时会需要用到调用第三方程序实现本系统的某一些功能,例如本文中需要使用到的swftools插件,那么如何在程序中使用这个插件,并且该插件是如何将PDF文件转化为SWF文件的呢?接下来 ...

  3. 浅析Java.lang.Process类

    一.概述      Process类是一个抽象类(所有的方法均是抽象的),封装了一个进程(即一个执行程序).      Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的 ...

  4. C#中Process类的使用

    本文来自: http://www.cnblogs.com/kay/archive/2008/11/25/1340387.html Process 类的作用是对系统进程进行管理,我们使用Process类 ...

  5. C#的Process类的一些用法

    c#之process类相关整理 一.根据进程名获取进程的用户名? 需要添加对 System.Management.dll 的引用 using System.Diagnostics; using Sys ...

  6. C#使用Process类调用外部程序(转)

    在程序开发中,一个程序经常需要去调用其他的程序,C#中Process类正好提供了这样的功能.它提供对本地和远程进程的访问并使您能够启动和停止本地系统进程.一.启动进程实例 Process myProc ...

  7. C# Process类_进程_应用程序域与上下文之间的关系

    进程(Process)是Windows系统中的一个基本概念,它包含着一个运行程序所需要的资源.进程之间是相对独立的,一个进程无法直接访问另一个进程的数据(除非分布式),一个进程运行的失败也不会影响其他 ...

  8. c#之process类相关整理

    一.根据进程名获取进程的用户名?   需要添加对 System.Management.dll 的引用   using System.Diagnostics; using System.Manageme ...

  9. C#代码使用Process类调用SWFTools工具

    一.Process类调用SWFTools工具将PDF文档转为swf文档 1 string cmdStr = "D:\\SWFTools\\pdf2swf.exe"; string ...

随机推荐

  1. js一些平时会用到的

    如何屏蔽页面js错误    <script language="javascript">    function killErrors()    {        re ...

  2. 应用emailAutoComplete.js来自动显示邮箱后缀列表

    我们经常有邮箱的人都特别清楚,在输入我们的邮箱时,会自动显示出邮箱后缀列表,这个用户体验是不错的. 操作据悉——当我们输入文字时,会自动有个邮箱后缀名的列表.      而我这边的代码是,应用jque ...

  3. Xcode中使用svn时,报证书验证错误Error validating server certificate for

    转:http://blog.csdn.net/yhawaii/article/details/7511141 今天使用Xcode自带的svn客户端时,总是连接不上服务器,报如下错误: Error va ...

  4. HDU 5818 Joint Stacks

    Joint Stacks Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. HDU 2056 Rectangles

    Rectangles Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. 1047图的深度优先遍历c语言

    描述 图(graph)是数据结构 G=(V,E),其中V是G中结点的有限非空集合,结点的偶对称为边(edge):E是G中边的有限集合.设V={0,1,2,……,n-1},图中的结点又称为顶点(vert ...

  7. DDOS的攻击原理和防护指南

    我们现在来分析DDOS的攻击原理. 首先,DDOS是英文Distributed Denial of Service的缩写,意思是分布式拒绝服务.拒绝服务又是什么意思呢?就是采取一些垃圾数据包来阻塞网站 ...

  8. Delphi 利用TComm组件 Spcomm 实现串行通信

    Delphi 利用TComm组件 Spcomm 实现串行通信 摘要:利用Delphi开发工业控制系统软件成为越来越多的开发人员的选择,而串口通信是这个过程中必须解决的问题之一.本文在对几种常用串口通信 ...

  9. Python中的__init__,__call__

    __init__函数 当一个类实例被创建时, __init__() 方法会自动执行,在类实例创建完毕后执行,类似构建函数.__init__() 可以被当成构建函数,不过不象其它语言中的构建函数,它并不 ...

  10. 在阿里云linux下使用SVN访问VisualSVN出错:SSL handshake failed: SSL error: Key usage violation in certificate has been detected

    Subversion clients receive the following error message when attempting to connect to VisualSVN Serve ...