• C# Process 类的思考


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

    下面我们用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();

    主要的工作已经完成了,下来就看你怎样利用程序的输入输出来完成一些功能了.

    在这里我也写了一个实现:

    Program代码  收藏代码
    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.        }  
  • 相关阅读:
    cosface: large margin cosine loss for deep face recognition
    Max-Mahalanobis Linear Discriminant Analysis Networks
    AT-GAN: A Generative Attack Model for Adversarial Transferring on Generative Adversarial Nets
    Generating Adversarial Examples with Adversarial Networks
    IMPROVING ADVERSARIAL ROBUSTNESS REQUIRES REVISITING MISCLASSIFIED EXAMPLES
    ADAM : A METHOD FOR STOCHASTIC OPTIMIZATION
    Universal adversarial perturbations
    7.15
    Python爬虫之urllib模拟登录及cookie的那点事
    Http协议
  • 原文地址:https://www.cnblogs.com/gc2013/p/3830084.html
Copyright © 2020-2023  润新知