Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to upload a file .When file is uploaded, I take some  information from it and then i want to delete it.

I manage to upload it, save it and get the info from it, but when i try to delete it i get the follwing exception "The process cannot access the file 'C:\Data\UploadedDocuments\81741092055.pdf' because it is being used by another process."


What I have tried:

C#
<pre>public async Task<IActionResult> SendMaillNewsLetter(IFormFile file,string subject,string fileName,string message)
        {
            

            tempGuid = Guid.NewGuid();
            ContactUs objUserContactUs = new ContactUs();
            var fileSavePath = _configuration.RootPath + fileName;
            if (file.Length > 0)
            {
                using (var stream = new FileStream(fileSavePath, FileMode.Create))
                {
                    await file.CopyToAsync(stream);
                }
            }

            List<string> NewsletterSubscriptionList = new List<string>();
            NewsletterSubscriptionList = _UserModel.GetNewsletterSubscriptionList(NewsletterSubscriptionList);
            if(NewsletterSubscriptionList.Count>0)
            {
                
                foreach (string item in NewsletterSubscriptionList)
                {
                    _emailSender.SendEmail(item, "", subject, message, fileSavePath);
                }

                             

                if (System.IO.File.Exists(fileSavePath))
                {
                    try
                    {
                        System.IO.File.Delete(fileSavePath);
                    }
                    catch (Exception ex)
                    {

                    }
                }

            }
            return Ok(new { objUserContactUs });
        }
Posted
Updated 22-Jan-20 18:07pm
Comments
Kornfeld Eliyahu Peter 22-Jan-20 6:04am    
Can you figure out what is the 'other process'?
Just for checking - instead using block, open and close the stream 'manually'...
F-ES Sitecore 22-Jan-20 6:55am    
Remove the "_emailSender.SendEmail" line and if the file is deleted ok then it is something inside your emailSender that is holding a reference to the file open.
murkalkiran 23-Jan-20 0:07am    
F-ES Sitecore
said its holinding in _emailSender.SendEmail sendemail logic after sending mail i have cleared the
var msg = new MailMessage();
like
msg.Attachments.Dispose();
murkalkiran 22-Jan-20 23:37pm    
after removing the _emailSender.SendEmail its deleting,but i need to send mail and the delete,one more thing i observed is i can't delete the file manually also when application is running ,if i stop the application and try to delete the file manually its deleting.how can i solve this issue.

The most likely process to be using it is you - Check the code where you save the file and make sure that you correctly close and dispose all file streams you use to create it before you try to copy / delete it.

The big question is "Why are you saving it at all if you are going to delete it?" File uploads are normally received as a byte array or stream so why save it at all?
 
Share this answer
 
Comments
murkalkiran 22-Jan-20 23:39pm    
i need to send a mail with attachment of uploaded file so i am saving the folder and then attaching the file from it's path ,is there any alternate to do this please let know thank you.
The issue solved as @
F-ES Sitecore 
said its holinding in _emailSender.SendEmail sendemail logic after sending mail i have cleared the
var msg = new MailMessage();

like
msg.Attachments.Dispose();
thanks
 
Share this answer
 

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