Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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);
            }
        }
C#

Posted
Updated 6-May-20 20:27pm

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.
 
Share this answer
 
Comments
murkalkiran 7-May-20 2:56am    
Thanks it worked after doing img.Dispose(); in convertImageTobytes function
manosabari 7-May-20 8:31am    
thanks
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.
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900