1

In thst code i have us two database tabele and fetch data in datatable then write all the data in the csv files with help of stream writer then add both the file in a folder and download in a zip form but sometimes its shows an error The process cannot access the file 'xxxxx.zip' because it is being used by another process.

  protected void btnExportToCSV_Click(object sender, EventArgs e)
    {
    //Work Detail
    blExportToExcel obj = new blExportToExcel();
    System.Data.DataTable dtTechSanc = blDbFunction.GetTechSanc(ddlAgency.SelectedValue.ToString(), ddlDivision.SelectedValue.ToString(), ddlDistrict.SelectedValue.ToString(), ddlMC.SelectedValue.ToString(), ddlScheme.SelectedValue.ToString(), ddlUserType.SelectedValue.ToString(), ddlWorkType.SelectedValue.ToString(), ddlWorkNature.SelectedValue.ToString(), ddlWorkStatus.SelectedValue.ToString(), ((prpSessionData)(Session["sUserInfo"])).UId);
    dtTechSanc.Columns["Id"].ColumnName = "WorkCode";
    dtTechSanc.Columns["TSId"].ColumnName = "Id";
    string filenamezip = "ZipFile\\TechSancRevision_" + DateTime.Now.ToString().Replace(":", "-").Replace("/", "-") + ".zip";
    string strPathAndQuery = HttpContext.Current.Request.PhysicalApplicationPath;
    string paths = strPathAndQuery + filenamezip;
    ZipFile z = ZipFile.Create(paths);
    z.BeginUpdate();

    if (dtTechSanc.Rows.Count > 0)
    {

        string tmpPath = Server.MapPath("FileTemplates");
        string tmpFileName = "TechSancRevision.csv";
        tmpPath = @"" + tmpPath.Replace("/", "\\").Replace("\\Department", "") + "\\" + tmpFileName;

        StreamWriter sw = new StreamWriter(tmpPath, false);
        int columnCount = dtTechSanc.Columns.Count;
        for (int i = 0; i < columnCount; i++)
        {
            sw.Write(dtTechSanc.Columns[i]);
            if (i < columnCount - 1)
            {
                sw.Write(",");
            }
        }
        sw.Write(sw.NewLine);
        foreach (DataRow dr in dtTechSanc.Rows)
        {
            for (int i = 0; i < columnCount; i++)
            {
                if (!Convert.IsDBNull(dr[i]))
                {
                    sw.Write(dr[i].ToString());
                }
                if (i < columnCount - 1)
                {
                    sw.Write(",");
                }
            }
            sw.Write(sw.NewLine);
        }
        sw.Close();

        z.Add(tmpPath, tmpFileName);
        z.CommitUpdate();

    }

    //Fund Allocation
    System.Data.DataTable dtFA = blDbFunction.GetFundAllocationTS(ddlAgency.SelectedValue.ToString(), ddlDivision.SelectedValue.ToString(), ddlDistrict.SelectedValue.ToString(), ddlMC.SelectedValue.ToString(), ddlScheme.SelectedValue.ToString(), ddlUserType.SelectedValue.ToString(), ddlWorkType.SelectedValue.ToString(), ddlWorkNature.SelectedValue.ToString(), ddlWorkStatus.SelectedValue.ToString(), ((prpSessionData)(Session["sUserInfo"])).UId);
    if (dtFA.Rows.Count > 0)
    {
        z.BeginUpdate();
        string tmpPath = Server.MapPath("FileTemplates");
        string tmpFileName = "FundsAllocationRevision.csv";
        tmpPath = @"" + tmpPath.Replace("/", "\\").Replace("\\Department", "") + "\\" + tmpFileName;
        dtFA.Columns["FAId"].ColumnName = "Id";
        dtFA.Columns["WorkId"].ColumnName = "WorkCode";
        StreamWriter sw = new StreamWriter(tmpPath, false);
        int columnCount = dtFA.Columns.Count;
        for (int i = 0; i < columnCount; i++)
        {
            sw.Write(dtFA.Columns[i]);
            if (i < columnCount - 1)
            {
                sw.Write(",");
            }
        }
        sw.Write(sw.NewLine);
        foreach (DataRow dr in dtFA.Rows)
        {
            for (int i = 0; i < columnCount; i++)
            {
                if (!Convert.IsDBNull(dr[i]))
                {
                    sw.Write(dr[i].ToString());
                }
                if (i < columnCount - 1)
                {
                    sw.Write(",");
                }
            }
            sw.Write(sw.NewLine);
        }
        sw.Close();

        z.Add(tmpPath, tmpFileName);
        z.CommitUpdate();
        z.Close();
    }
       System.IO.FileInfo file = new System.IO.FileInfo(paths);
       Response.ContentType = "application/text";
       Response.AddHeader("Content-Disposition", "attachment;filename=\"" + Path.GetFileName(paths));
       Response.ContentType = "application/octet-stream";
       Response.WriteFile(file.FullName);/// error shows here
       Response.End();
 }
}
sandeep singh
  • 143
  • 1
  • 13
  • If two users start this at the same time (within one second) the resulting zip file will have the same name. That could be the reason. But I suppose it should throw an exception earlier if that happens... – user1429080 Oct 13 '14 at 10:24

1 Answers1

0

I am not sure this will help you or not but i am transmitting zip using below code , please try it. It works perfectly and in use from past 2 years.

        Response.ContentType = "application/zip";
        Response.AppendHeader("Content-Disposition", "attachment; filename=Test.zip")
        Response.TransmitFile(@"C:\Test.zip");
        Response.Flush();
        // Prevents any other content from being sent to the browser
        Response.SuppressContent = true;
        // Directs the thread to finish, bypassing additional processing
        HttpContext.Current.ApplicationInstance.CompleteRequest();
manthan davda
  • 319
  • 1
  • 3
  • 13