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.