am doing application of downloading images using wget process in c#. But while downloading images I am providing type of image i.e. .jpeg or .png. Through wget command. If while downloading I am passing .jpeg and if .jpeg is not present, Then I want to trap error "file not found" through process class. But it is not happening.
My code is below:
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();
}
}
In above code if jpeg is not present then in erroe.log coming "file not found". But through process class not trapping error i.e. in ProcessErrorData e.Data is always coming null. So how can I trap error is there any other way?
Any Help is appreciated.
wget (in versions above 1.12) does return a reliable exitcode.
Prior to 1.12 you're in trouble:
The exitcode of the process is handed back to the Process instance in the ExitCode property. You should leverage that and keep the (error) logging for convenience.
If you absolutely want to respond to the error messages that are logged (either to standardoutout or erroroutput), make sure you check the correct strings: If the log shows
ERROR 404: Not Found
then thismessage.Contains("file not found")
will never be true. This is a reason I always tend to stay away from logfile parsing.Do notice that processes are not required to write errormessage to the standard error stream. The errormessages can very well come in the standard output stream as well...