2015-04-22 56 views
0

我想知道如何与Mono进行shell交互,我似乎无法找到关于此的很多信息。例如,我想返回“ls”的输出并将其粘贴到变量中 - 这甚至可能吗?如何获得Mono C中Shell命令的返回值#

这是我到目前为止有:

 var proc = new Process(); 
     proc.StartInfo.FileName = "ls"; 
     proc.Start(); 
     proc.Close() 

回答

0

有可能获得外壳输出。请尝试以下内容 -

Process p = new Process(); 
p.StartInfo = new ProcessStartInfo("/bin/ls", "-l") 
        { 
        RedirectStandardOutput = true, 
        UseShellExecute = false 
        }; 
p.Start(); 
p.WaitForExit(); 
//The output of the shell command will be in the outPut variable after the 
//following line is executed 
var outPut = p.StandardOutput.ReadToEnd();