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 8347093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:15:05+00:00 2026-06-09T07:15:05+00:00

I’m getting the error message at the end, when I try to delete files

  • 0

I’m getting the error message at the end, when I try to delete files in the “foreach” loop.

I know I need to be using the keyword ´using` somewhere but I’m not sure where and how.

private void btnEmailIntegrationFiles_Click(object sender, EventArgs e)
{
    DialogResult EmailWarningMsg = MessageBox.Show("You're about to email the Integration IAT text files. Are you sure?", "WARNING!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);

    if (EmailWarningMsg == DialogResult.Yes)
    {
        if (Directory.GetFiles(AppVars.NetworkIntegrationFileLocation).Length == 0)
        {
            MessageBox.Show("The folder is empty. Please create the files before sending it.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            if (Directory.GetFiles(AppVars.NetworkIntegrationFileLocation).Length != 4)
            {
                MessageBox.Show("The folder does not contain exactly 4 files.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                Email email = new Email();

                email.SendEmailToFinalDestinationWithAttachments(AppVars.DBTeamEmail, AppVars.ChrisWhitmoreEmail, AppVars.DBTeamEmail, "Integration Files", "Please see integration files attached.");
            }
        }

        PopulateListViewWithPoliciesAvailableToHoldBack();

        string[] files = Directory.GetFiles(AppVars.NetworkIntegrationFileLocation);

        foreach (string file in files)
        {
            File.Delete(file);
        }
    }
}

How would I go about using the keyword using for “emailing” so that I don’t run into this error message when trying to delete those files that were emailed as attachments?

Here’s the send email class:

public void SendEmailToFinalDestinationWithAttachments(string EmailFrom, string EmailTo, string EmailCC, string EmailSubject, string EmailBody)
        {
            try
            {
                MailMessage EmailMessage = new MailMessage();
                SmtpClient smtp = new SmtpClient(AppVars.SMTPClient, AppVars.SMTPClientPort);
                smtp.UseDefaultCredentials = false;
                EmailMessage.IsBodyHtml = true;
                EmailMessage.To.Add(EmailTo);
                EmailMessage.CC.Add(EmailCC);
                EmailMessage.CC.Add(user);
                EmailMessage.Subject = EmailSubject;
                EmailMessage.From = new MailAddress(EmailFrom);
                EmailMessage.Body = EmailBody;

                string[] files = Directory.GetFiles(AppVars.NetworkIntegrationFileLocation, "*" + DateTime.Now.ToString("yyyyMMdd") + "*");

                foreach (string file in files)
                {
                    EmailMessage.Attachments.Add(new Attachment(file));
                }

                smtp.Send(EmailMessage);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
  • 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-09T07:15:07+00:00Added an answer on June 9, 2026 at 7:15 am

    You need to modify your EMail class to be something like:

    using (MailMessage EmailMessage = new MailMessage()) {
        ...
        smtp.Send(EmailMessage);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I need to clean up various Word 'smart' characters in user input, including but
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

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.