0

我如何使用.net捕获腻子中发生的异常

我正在做的是使用 .net 代码将一些 shell 脚本文件发送到 putty 现在我想捕获脚本文件中发生的任何错误并使用我的 .net 代码处理它。知道如何做到这一点

`try
        {
            StringBuilder outputBuilder = new StringBuilder();
            //outputBuilder = null;
            string CommandSetFileName = "D://POC.txt";
            Process objProcess = new Process();
            //objProcess.StartInfo.FileName = "putty.exe";
            //objProcess.StartInfo.Arguments = txtUserName.Text.Trim() + "@" + txtIP.Text.Trim() + " -pw " + txtPassword.Text.Trim() + " -m " + CommandSetFileName;

            ProcessStartInfo objInfo = new ProcessStartInfo();
            objInfo.FileName = "putty.exe";
            objInfo.Arguments = txtUserName.Text.Trim() + "@" + txtIP.Text.Trim() + " -pw " + txtPassword.Text.Trim() + " -m " + CommandSetFileName;
            objInfo.UseShellExecute = false;
            objInfo.RedirectStandardError = true;
            objInfo.RedirectStandardOutput = true;
            objInfo.RedirectStandardInput = true;
            objProcess.StartInfo = objInfo;

            objProcess.EnableRaisingEvents = true;
            // attach the event handler for OutputDataReceived before starting the process 
            objProcess.OutputDataReceived += new DataReceivedEventHandler(delegate(object sender, DataReceivedEventArgs e)
                {         // append the new data to the data already read-in         
                    outputBuilder.Append(e.Data);
                }
                    );

            objProcess.Start();

            objProcess.BeginOutputReadLine(); 
            objProcess.WaitForExit(); 
            objProcess.CancelOutputRead();  // use the output 
            string output = outputBuilder.ToString(); 



            //string output = objProcess.StandardOutput.ReadLine();
            objProcess.WaitForExit();

            string error = objProcess.StandardError.ReadLine();

            objProcess.WaitForExit();
        }
        catch (Exception e)
        {
            MessageBox.Show("Unable to connect UNIX", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }`

这是我正在使用的代码

4

1 回答 1

0

如果您想捕获发送/复制时可能发生的任何错误,您可以尝试以下操作:(我对腻子不太熟悉)

try
{
  //Send scripts
}
catch(Exception ex)
{
  //Handle error
}
finally
{
  //Cleanup resources if necessary
}

注意:建议捕获特定的异常类型而不是Exception,上面的代码只是一个示例。

如果您想处理脚本执行期间发生的错误,那么上面的代码可能没有帮助。

你能提供任何进一步的信息吗?

于 2012-08-01T13:35:24.277 回答