if file not found, skip to next file

Apr 20, 2010 at 5:56 PM

Hi Cheso,

The following code I've created from examples around the DotNetZip disscussion forums is working under a normal app environment for us:

    protected void makeZip(object sender, EventArgs e)
    {
        string archiveTitle = (poYearDropDownList.SelectedItem.Value + poProgramDropDownList.SelectedItem.Value).ToString();

        string fileName = String.Empty;
        string ZipFileName = Server.MapPath(Path.Combine("zipFiles", Guid.NewGuid().ToString() + ".zip"));
        string pathToFile = ("z:\\this\\is\\not\\theReal\\filePath\\"); 
       
        using (ZipFile POzip = new ZipFile())
        {
             foreach (GridViewRow PO in GridView1.Rows)
            {
              fileName = (PO.FindControl("POLabel") as Label).Text;
              POzip.AddFile(pathToFile + fileName, "");
            }

            POzip.Save(ZipFileName);
        }

        Response.Clear();
        Response.BufferOutput = false;
        Response.ContentType = "application/zip";
        string archiveName = String.Format(archiveTitle + "-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + archiveName);
        Response.WriteFile(ZipFileName);
        Response.Flush();
        Response.Close();

        // delete the temp file
        if (File.Exists("zipFiles/" + ZipFileName))
            File.Delete("zipFiles/" + ZipFileName);

However - for reasons that I'll not go into here - on occasion there will be a "file not found" - in that - the file name listed in the gridView will not actually exsist in the directory where all the files can be found.

Is there a way (in C#) that you know of to "skip the file" then move on to the next file in my foreach loop?  I'm assuming an If statement is needed, but am drawing a blank on where to start.

Thanks for such a great zip library!

-Doug

 

Apr 20, 2010 at 6:07 PM

Well, duh, I just figured this out - and added:

if(File.Exists(pathToFile + fileName))

 {

 POzip.AddFile(pathToFile + fileName, "");

}