15 Ocak 2018 Pazartesi

Process Sınıfı ile Okuma

Örnek - ReadToEnd
Şöyle yaparız.
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"c:\windows\system32\cmd.exe";
p.StartInfo.Arguments = "/c dir c:\*.iso /s /b";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Örnek - ReadToEnd
Şöyle yaparız.
using (Process proc = Process.Start (...)) {
  using (StreamReader reader = proc.StandardOutput) {
    string result = reader.ReadToEnd ();
    ...
  }
}
Örnek - Satır satır okuma
Elimizde şöyle bir process olsun.
var proc = new Process {
  StartInfo = new ProcessStartInfo {
    FileName = "python.exe whatever.py",
    Arguments = "yourargs",
    UseShellExecute = false,
    RedirectStandardOutput = true,
    CreateNoWindow = true
  }
};
Şöyle yaparız.
proc.Start();
StringBuilder bldr = new StringBuilder();
while (!proc.StandardOutput.EndOfStream) {
    bldr.append(proc.StandardOutput.ReadLine()+"\n");
}

Hiç yorum yok:

Yorum Gönder