I have some code to download a text file from a website. When the requested file does not exist, my application downloads a text file which has html content. I need to filter this html content (should not download a text file with html content if the requested file does not exist) and need to download only text files which has the correct content. Below is my code.
string FilePath = @"C:\TextFiles\" + FileName + String.Format("{0:00000}", i) + ".TXT";
Directory.CreateDirectory(Path.GetDirectoryName(FilePath));
//MessageBox.Show(FilePath);
using (FileStream download = new FileStream(FilePath, FileMode.Create))
{
Stream stream = clientx.GetResponse().GetResponseStream();
while ((read = stream.Read(buffer, 0, buffer.Length)) != 0)
{
download.Write(buffer, 0, read);
}
}
Please advice
Assuming
clientxisHttpWebRequestthen just check the StatusCode of the response: