System.IO.FileNotFoundException for files to zip

Oct 17, 2010 at 1:02 AM

Hi guys,

Totally hit the wall and cant get my head around the issue. Basically I built a simple ASP.NET (C#) app, based on the example, which reads files from a particular folder within the VS2010 project, fills the checkbox list with the file names, then you tick appropriate boxes to indicate which files you want to zip. Once download button is pressed the resulting zip file is prepared and sent back by the browser.

The problem I am hitting is that when the download button is pressed I get the following Exception:

System.IO.FileNotFoundException: C:\Program Files\Common Files\Microsoft Shared\DevServer\10.0\test.pdf, test.pdf is one of the files which I tick to include in the zip. The VS2010 informs me that the exception corresponds to the line where this code resides - zfZip.Save(Response.OutputStream);

So for some reason "something" is looking into the DevServer\10.0\ folder for files to zip instead of the  Downloads directory within the project. I am not not quite sure why this is happening, perhaps something needs to be configured, but I am not sure what...

I'd be grateful if someone could point me in a right direction of where to dig to find a solution for the issue.


 

public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath("~/Downloads/"));
                cblFiles.DataSource = dirInfo.GetFiles();
                cblFiles.DataBind();

            }
        }

        protected void btnDownload_Click(object sender, EventArgs e)
        { 
            string message = "You must select one or more files to download."; string strZipFileName; string strReadme; if (cblFiles.SelectedItem == null) { ClientScript.RegisterStartupScript( this.GetType(), Guid.NewGuid().ToString(), string.Format("alert('{0}');", message.Replace("'", @"\'")),true); return; } strZipFileName = string.Format("Download - {0}.zip", DateTime.Now.ToString("yyyy-MM-dd-HH_mm_ss")); Response.ContentType = "application/zip"; Response.AddHeader("Content-Disposition", "filename="+strZipFileName); using (ZipFile zfZip = new ZipFile()) { if (!string.IsNullOrEmpty(txtZipPassword.Text)) { zfZip.Password = txtZipPassword.Text; zfZip.Encryption = EncryptionAlgorithm.WinZipAes128; } strReadme = string.Format("Your ZIP file {0} contains the following files:{1}{1}", strZipFileName,Environment.NewLine); foreach (ListItem li in cblFiles.Items) { if (li.Selected) { strReadme += string.Concat("\t*", li.Text, Environment.NewLine); zfZip.AddFile(li.Value); } } zfZip.AddEntry("readme.txt", strReadme); zfZip.Save(Response.OutputStream); } } }

 

 

 

Oct 18, 2010 at 10:52 AM

Hi Andrew,

Is your "li.Value" a fully qualified path, or just a file name? I've not looked at the DotNetZip source code to confirm, but I'm guessing AddEntry will use the current working directory if you don't specify which one the file is in. In your case this is "C:\Program Files\...\DevServer\10.0".

You might need to use something like this instead:

zfZip.AddFile(Server.MapPath("~/Downloads/") + li.Value);

Hope this helps,

Mike

Oct 18, 2010 at 11:42 AM

Hi Mike,

Thanks for your reply, worked perfectly after I added - "Server.MapPath("~/Downloads/") +" since "li.Value" did contain only the filename.

Cheers for that buddy!!!

 

Andrew