2017-09-26 112 views
0

Im在CentOs上運行dotnet核心控制檯應用程序。以下代碼正在執行正常命令liki「正常運行時間」。但不執行的「LZ4 -dc --no稀疏vnp.tar.lz4 |焦油XF - Logs.pdf」使用dotnet核心在Centos上執行linux命令

try 
     { 
     var connectionInfo = new ConnectionInfo("server","username",new PasswordAuthenticationMethod("username", "pwd")); 
      using (var client = new SshClient(connectionInfo)) 
      { 
       client.Connect(); 
           Console.WriteLine("Hello World!"); 
       var command=client.CreateCommand("lz4 -dc --no-sparse vnp.tar | tar xf - Logs.pdf"); 
       var result = command.Execute(); 
       Console.WriteLine("yup ! UNIX Commands Executed from C#.net Application");     
       Console.WriteLine("Response came form UNIX Shell" + result); 

       client.Disconnect(); 
      } 
     } 
     catch (Exception ex) 
     { 

      Console.WriteLine(ex.Message); 
     } 

預計產量Logs.pdf文件需要被提取並保存在當前位置。有人可以糾正我在哪裏IM

回答

0

如果應用程序是在Linux機器上運行,那麼你可以試試這個也:

string command = "write your command here"; 
string result = ""; 
using (System.Diagnostics.Process proc = new System.Diagnostics.Process()) 
{ 
    proc.StartInfo.FileName = "/bin/bash"; 
    proc.StartInfo.Arguments = "-c \" " + command + " \""; 
    proc.StartInfo.UseShellExecute = false; 
    proc.StartInfo.RedirectStandardOutput = true; 
    proc.StartInfo.RedirectStandardError = true; 
    proc.Start(); 

    result += proc.StandardOutput.ReadToEnd(); 
    result += proc.StandardError.ReadToEnd(); 

    proc.WaitForExit(); 
} 
return result;