Todd Burrell

Todd Burrell

  • NA
  • 15
  • 0

Check To See if File Exits, Before System.IO.FileStream

Jan 1 2010 6:19 AM
Happy New Year from a new guy here, who is hoping for a little help.

I have a web form, which after querying a database, displays a list of available PDF files in a DataList.  The DataList items are links to the PDF documents in the file system, and the requested PDF document to be opened in a new page using System.IO.Filestream.

string filename1 = Request.QueryString["Doc_Name"].ToString();
string dir = filename1.Substring(3, 4);
atring mth = filename1.Substring(7,2);

Response.Clear();
Response.Buffer = true;
Response.ContentType = @"application/pdf";
System.IO.FileStream myFileStream = new System.IO.FileStream((@"D:/" + dir + '/' + mth + '/' + filename1), System.IO.FileMode.Open);
long FileSize = myFileStream.Length;
byte[] Buffer = new byte[(int)FileSize];
myFileStream.Read(Buffer, 0, (int)FileSize);
myFileStream.Close();
Response.BinaryWrite(Buffer);
Response.Flush();
Response.End();


In some cases there may be a disconnect between the filename in the database and the file in the directory (like if someone accidentally deleted the target file).  My question is, how can I build in a check to determine that the file actually exists before the FileStream, so that I can return an error message?

Thanks.

Answers (2)