C# 如何捕获wget进程的错误?
我正在用c#中的wget进程下载图像。 但在下载图像时,我提供了图像类型,即jpeg或.png。通过wget命令。 如果下载时我正在传递.jpeg,并且如果.jpeg不存在,那么我希望通过process类捕获错误“未找到文件”。但事实并非如此 我的代码如下:C# 如何捕获wget进程的错误?,c#,wget,C#,Wget,我正在用c#中的wget进程下载图像。 但在下载图像时,我提供了图像类型,即jpeg或.png。通过wget命令。 如果下载时我正在传递.jpeg,并且如果.jpeg不存在,那么我希望通过process类捕获错误“未找到文件”。但事实并非如此 我的代码如下: class TrapProcessError { private Process process = new Process(); public TrapProcessError() { } pu
class TrapProcessError
{
private Process process = new Process();
public TrapProcessError()
{
}
public void Start()
{
string args= "-r -c -np --retry-connrefused --tries=0 "url containing image folder" -P C:\\Images --cut-dirs=2 -nH -A jpeg -o C:\\Images\\error.log";
string filename= "path of wget\\wget.exe";
process.StartInfo.FileName = filename;
process.StartInfo.Arguments = args;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.ErrorDataReceived += this.ProcessErrorData;
process.OutputDataReceived += this.ProcessOutputData;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
}
private void ProcessErrorData(object sender, DataReceivedEventArgs e)
{
string message = e.Data;
if (message.Contains("file not found"))
{
Console.WriteLine("Error :" +message);
process.Close();
}
}
private void ProcessOutputData(object sender, DataReceivedEventArgs e)
{
string message = e.Data;
Console.WriteLine("Output :" +message);
}
public static void Main(string[] args)
{
TrapProcessError trapProcessError= new TrapProcessError();
trapProcessError.Start();
}
}
在上面的代码中,如果jpeg不存在,则在errore.log中出现“未找到文件”。但通过进程类不捕获错误,即在ProcessErrorData中,数据总是为空。那么,我如何捕捉错误呢?还有其他方法吗
非常感谢您的帮助。wget(在1.12以上的版本中)确实会返回一个可靠的消息
0未发生任何问题。1个通用错误代码。
2解析错误例如,在解析命令行选项时,“.wgetrc”或“.netrc”…
3文件I/O错误。
4网络故障。
5 SSL验证失败。
6用户名/密码身份验证失败。
7个协议错误。
8服务器发出了错误响应 在1.12之前,您遇到了麻烦: 在1.12之前的Wget版本中,Wget的退出状态往往没有帮助且不一致。不管遇到什么问题,递归下载实际上总是返回0(成功),而非递归回迁只返回与最近尝试的下载相对应的状态 流程的exitcode将在属性中传递回流程实例。为了方便起见,您应该利用这一点并保持(错误)日志记录
public void Start()
{
string args= "-r -c -np --retry-connrefused --tries=0 "url containing image folder" -P C:\\Images --cut-dirs=2 -nH -A jpeg -o C:\\Images\\error.log";
string filename= "path of wget\\wget.exe";
process.StartInfo.FileName = filename;
process.StartInfo.Arguments = args;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.RedirectStandardOutput = true;
process.ErrorDataReceived += this.ProcessErrorData;
process.OutputDataReceived += this.ProcessOutputData;
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
if (process.ExitCode > 0)
{
// do what you need to do in case of an Error
Console.WriteLine("Error occured:{0}", process.ExitCode);
}
}
如果您绝对希望响应记录的错误消息(standardoutout或erroroutput),请确保检查正确的字符串:如果日志显示error 404:Not Found
,则此message.Contains(“file Not Found”)
将永远不会为真。这就是我总是倾向于远离日志文件解析的原因
请注意,不需要进程将errormessage写入标准错误流。错误消息也可以很好地出现在标准输出流中…:我已经尝试过了。但是当进程因“未找到”异常而终止时。ExitCode仅为零。这是不明白的,为什么它会变成零?是的,它正在发送http 404错误代码。wget.exe的版本为1.11.4请将wget升级到高于1.12“HTTP请求已发送,等待响应…404未找到”的版本2014-11-01 16:05:56错误404:未找到。删除“文件路径\\image.png”,因为它应被拒绝。“我已搜索wget版本1.12。。但发现“wget 1.12,但该版本目前不适用于Windows”