我有一个运行 .bat
和 .vbs
文件的控制台应用程序。
启动这些进程的方法如下:
public void runProcess(string aPath,string aName,string aFiletype)
{
string stInfoFileName;
string stInfoArgs;
if(aFiletype == "bat")
{
stInfoFileName = (@aPath + @aName);
stInfoArgs = string.Empty;
}
else
{ //vbs
stInfoFileName = @"cscript";
stInfoArgs = "//B //Nologo " + aName;
}
this.aProcess.StartInfo.FileName = stInfoFileName;
this.aProcess.StartInfo.Arguments = stInfoArgs;
this.aProcess.StartInfo.WorkingDirectory = @aPath;
this.aProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
this.aProcess.Start();
Console.WriteLine("information passed from batch: ???");
this.aProcess.WaitForExit(); //<-- Optional if you want program running until your script exit
this.aProcess.Close();
}
它正在启动的当前 .bat
文件用于通过 ftp 发送数据(ftp
、ftps
或 sftp
)。
当进程运行时,所有信息都显示在进程窗口中,例如可能会发生错误,文件不会传输,并且会在该“子”进程窗口中显示详细信息。该过程完成后,“子”窗口将随消息一起消失。
我能否以某种方式将这些有用信息返回到我的主控制台应用程序窗口?
最佳答案
检查进程退出代码 ( Process.ExitCode
) 和/或捕获控制台输出。
参见 Capturing console output from a .NET application (C#)
尽管您最好使用一些 native .NET SFTP/FTP 库。
对于 SFTP,请参阅 SFTP Libraries for .NET
对于 FTP 和 FTPS,可以使用 FtpWebRequest
来自 .NET 框架。
如果您想要一体化解决方案,我可以谦虚地向您推荐我的 WinSCP .NET assembly .使用程序集,您可以为 SFTP、FTP 和 FTPS 使用相同的代码。在内部,程序集实际运行 WinSCP 控制台应用程序。所以它做的事情与您尝试自己编写代码的事情相同。
关于c# - 将信息传回控制台应用程序窗口的批处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30889247/