Process p = new Process();
p由我的程序启动
怎样把进程p的标准输出直接重定向到一个指定的文本文件?
怎样阻止p的所有文件操作?
或者监视也可以,最好能在操作实施前获取。

解决方案 »

  1.   

    在dos命令下重定向输出到文件一般用" >xxx.txt"参数
    如: ipconfig >c:\output.txt阻止p的所有文件操作可能要用到系统的API,请楼下继续。
      

  2.   

    ProcessStartInfo psi = new ProcessStartInfo(filepath);
                psi.RedirectStandardOutput = true;
                Process p = Process.Start(psi);
                p.StandardOutput = yourConsole; // yourConsole is the StreamReader for your target input stream阻止一个外部程序的所有文件操作据我所知不可能,所以你不如将其输出重定向到自己的流中,想要log的话就让这个流导向文件,否则干脆满了就清空
      

  3.   

    不好意思,有点误导,这里的
    p.StandardOutput = yourConsole; // yourConsole is the StreamReader for your target input stream
    应该写成:
    StreamReader sr = p.StandardOutput;
    StreamWriter sw = new StreamWriter(logFilePath);
    sw.Write(sr.ReadToEnd());
    sw.Close();以上只是此类代码的写法,我建议你的实际程序还是用多线程来做这种东西。一般情况下如果输出流被填满,子进程p会被阻塞直至父进程取走流中数据。小心引起死锁~ 更具体的东西可以到msdn上查查