Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:38:26+00:00 2026-06-12T08:38:26+00:00

I am creating a file in local drive with content by using below code.

  • 0

I am creating a file in local drive with content by using below code.

File.WriteAllLines(path, contents);

I am attaching this file to a mail and sending acrross the team. Once mail sent I need to delete the file, to delete the file I am using below code but I am getting runtime error

File.Delete(path);

Error Message : The process cannot access the file, because it is being used by another process

by default WriteAllLines() method closes the file but still it is opened by another process. I can only delete the file by executing the code after sometime but this is not the scenario. I need to delete it once the mail has been sent.

Update

System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();

       mailMessage.To.Add(new System.Net.Mail.MailAddress(recipient, ToName));

       mailMessage.From = new System.Net.Mail.MailAddress(From, FromName);
       mailMessage.Subject = Subject; // "Outlook calendar as attachment";  // modified by Srikanth J on 28/06/2012
       mailMessage.Body = "This is a test message";

       System.Net.WebClient webclient = new System.Net.WebClient();

       webclient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

       for (int i = 0; i < item.Attachments.Count; i++)
       {
           string url = item.Attachments.UrlPrefix + item.Attachments[i];
           SPFile file = item.ParentList.ParentWeb.GetFile(url);
           mailMessage.Attachments.Add(new System.Net.Mail.Attachment(file.OpenBinaryStream(), file.Name));

       }

       System.Net.Mail.Attachment mailAttachment = new System.Net.Mail.Attachment(path);

       mailMessage.Attachments.Add(mailAttachment);
       smtp.Send(mailMessage);

Any help is appriciated, Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:38:28+00:00Added an answer on June 12, 2026 at 8:38 am

    MailMessage implements IDisposable, so you should be using the using keyword to release any resources when you’re done with it. If you don’t do that, yes, it’s very well possible that the file remains in use until the garbage collector happens to notice that you’re not using the message anymore.

    using (var mailMessage = new MailMessage())
    {
       // set mailMessage properties
       // ...
       smtp.Send(mailMessage);
    }
    

    You could also call Dispose for the attachment directly, but disposing of a message will already ensure all subobjects, including attachments, get disposed correctly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating excel file using below code public class ResultSetToExcel { private HSSFWorkbook
I tried creating a pdf file out of another one(in my local drive) using
I am creating a file with the content depending on some local variables. I
I am creating a file manager application using vaadin as ui and alfresco as
I am creating Excel file download using NOPI libraries. I have lot of data
I am creating a file from a SELECT query using sqlplus with SPOOL command.
I'm creating a python script which accepts a path to a remote file and
I have written the following code to write a file on my local file
I have a path to an Excel file which is located on my local
I started creating file downloader class. Here is what i have: public class Downloader

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.