Click here to Skip to main content
14,666,459 members
Rate this:
Please Sign up or sign in to vote.
See more:
hi, I am trying to delete a file from folder using c#

I am reading an excel chart from the excel and the converting chart into the image and saved in local folder for other use , once I use the image now I want to delete that image by providing the path but I am getting error like "
System.IO.IOException: 'The process cannot access the file 'D:\chart\86e740b9-83bc-470e-9e03-efa9864b1d8e.jpg' because it is being used by another process.'
"

What I have tried:

{
Workbook wb = new Workbook("D:\\chart\\Qchart.xlsx");

            // Access the first worksheet
            Worksheet worksheet = wb.Worksheets[0];
            StringBuilder Reportbuilder = new StringBuilder();
            Reportbuilder.Append("<html>");
            Reportbuilder.Append("<header>");
            Reportbuilder.Append("</header>");
            Reportbuilder.Append("<body>");
            Reportbuilder.Append("<table style='width:100%;' >");
            foreach (var sec in SectionList)
            {
                Guid secGuid;

                secGuid = Guid.NewGuid();

                worksheet.Cells["D6"].PutValue(sec.PerformanceScore);
                // Set the print area with your desired range
                worksheet.PageSetup.PrintArea = "AH4:BG25";

                // Set all margins as 0
                worksheet.PageSetup.LeftMargin = 0;
                worksheet.PageSetup.RightMargin = 0;
                worksheet.PageSetup.TopMargin = 0;
                worksheet.PageSetup.BottomMargin = 0;

                // Set OnePagePerSheet option as true
                ImageOrPrintOptions options = new ImageOrPrintOptions();
                options.OnePagePerSheet = true;
                //options. = ImageFormat.Jpeg;
                options.HorizontalResolution = 200;
                options.VerticalResolution = 200;

                // Take the image of your worksheet
                SheetRender sr = new SheetRender(worksheet, options);
                sr.ToImage(0, "D:\\chart\\"+secGuid+".jpg");
                byte[] Imge = convertImageTobytes("D:\\chart\\" + secGuid + ".jpg");
               
                Reportbuilder.Append("<tr>'"+sec.SectionName+"'");
                Reportbuilder.Append("</tr>");
                Reportbuilder.Append("<tr><img   src='data:image/png;base64," + @System.Convert.ToBase64String(Imge) + "'/> </tr>");
                DeleteImage("D:\\chart\\" + secGuid + ".jpg");
                foreach (var question in sec.ListQuestions)
                {

                }
            }

            Reportbuilder.Append("</body>");
            Reportbuilder.Append("</table>");





            return char111t;
        }

        public byte[] convertImageTobytes(string path)
        {
            System.Drawing.Image img = System.Drawing.Image.FromFile(path);
            byte[] bytes;
            using (MemoryStream ms = new MemoryStream())
            {
                img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                bytes = ms.ToArray();
                ms.Dispose();
                ms.Close();
            }
            return bytes;
        }

        public void DeleteImage(string path)
        {
            if ((System.IO.File.Exists(path)))
            {
                System.IO.File.Delete(path);
            }
        }

		    
Posted
Updated 6-May-20 20:27pm

Rate this:
Please Sign up or sign in to vote.

Solution 2

For System.Drawing.Image.FromFile see here Image.FromFile Method (System.Drawing) | Microsoft Docs[^]in the remarks:
The file remains locked until the Image is disposed.
   
Comments
murkalkiran 7-May-20 2:56am
   
Thanks it worked after doing img.Dispose(); in convertImageTobytes function
manosabari 7-May-20 8:31am
   
thanks
Rate this:
Please Sign up or sign in to vote.

Solution 1

Which word you don't understand in
System.IO.IOException: 'The process cannot access the file 'D:\chart\86e740b9-83bc-470e-9e03-efa9864b1d8e.jpg' because it is being used by another process.'

By design, the OS forbid you from deleting a file in use.
Solution, wait until the file is not in use anymore.
   
Comments
murkalkiran 7-May-20 1:42am
   
I can delete the image file manually before this line byte[] Imge = convertImageTobytes("D:\\chart\\" + secGuid + ".jpg"); execute
but after converting yo byte array I am not able to delete manually also, I try to delete manually it's showing "The action can't be completed because the file is open in .Net Core Host"
Patrice T 7-May-20 1:54am
   
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100