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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:44:33+00:00 2026-06-04T01:44:33+00:00

Sub Main() Try Dim output, filename1, filename2, filename3, date1, date2 As String ‘today’s final

  • 0
 Sub Main()
            Try
                Dim output, filename1, filename2, filename3, date1, date2 As String

                'today's final
                output += "Report Dates: " & date1 & " and " & date2
                filename1 = "filename1.doc"
                SaveToFile(output, filename1)


                'today's daily
                output = "Report Dates: " & date1 & " and " & date2
                filename2 = "filename2.doc"
                SaveToFile(output, filename2)

                'yesterday's final
                output = "Report Dates: " & date1 & " and " & date2
                filename3 = "filename3.doc"
                SaveToFile(output, filename3)

    'email files here
    SendEmail(to, body,date1);

                'detele temp files
                DeleteFile(filename1)
                DeleteFile(filename2)
                DeleteFile(filename3)
            Catch e As Exception
                cEmail.SendErrorEmail("me@hme.com", e.Message)
            End Try

    End Sub

Sub SaveToFile(ByVal text As String, ByVal fileName As String)
        Dim path As String = "c:\temp\" & fileName
        Try
            If File.Exists(path) = True Then
                File.Delete(path)
            End If

            ' Create a file to write to.
            Dim sw As StreamWriter = File.CreateText(path)
            sw.WriteLine(text)
            sw.Flush()
            sw.Close()

            ' Open the file to read from.
            Dim sr As StreamReader = File.OpenText(path)
            Do While sr.Peek() >= 0
                Console.WriteLine(sr.ReadLine())
            Loop
            sr.Close()
        Catch e As Exception
            message = "in SaveToFile  " & e.Message
            cEmail.SendErrorEmail("me@hme.com", message)
        End Try
    End Sub



Sub DeleteFile(ByVal fileName As String)
        Dim path As String = "c:\temp\" & fileName
        Try

            If File.Exists(path) = True Then
                File.Delete(path)
            End If

        Catch e As Exception
            message = "in DeleteFile  " & e.Message
            cEmail.SendErrorEmail("me@hme.com", message)
        End Try
    End Sub

i’m getting the following error:

in DeleteFile The process cannot access the file
‘c:\temp\filename2.doc’ because it is being used by another process.

am i supposed to release any processes before deleting the files? what am i missing?

EDIT: here is my “Send Email” function that sends out the files

 Public Sub SendEmail(ByVal msgTo As String, ByVal msgBody As String, ByVal date1 As String)


        Dim mail As New MailMessage()
        Dim objSMTP As New SmtpClient()
        Dim filename As String

        ''''''''''''''''''''''''''''''''''''''


        Try
            mail.To.Add(msgTo)
            mail.Bcc.Add("me@hme.com")
            mail.Priority = MailPriority.Normal
            mail.IsBodyHtml = True
            mail.Subject = "subject"
            mail.Body = msgBody


            filename = "filename1.doc"
            Dim DOERecords As New Net.Mail.Attachment("C:\temp\" & filename) 'create the attachment
            filename = "filename2.doc"
            Dim FOERecords As New Net.Mail.Attachment("C:\temp\" & filename) 'create the attachment
            filename = "filename3.doc"
            Dim FOERecords2 As New Net.Mail.Attachment("C:\temp\" & filename) 'create the attachment


            mail.Attachments.Add(DOERecords) 'add the attachment
            mail.Attachments.Add(FOERecords) 'add the attachment
            mail.Attachments.Add(FOERecords2) 'add the attachment

            objSMTP.Send(mail)

        Catch ex As Exception
            Throw ex
        End Try
    End Sub
  • 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-04T01:44:35+00:00Added an answer on June 4, 2026 at 1:44 am

    You need to dispose all Attachment objects, or you will leave open file handles behind.
    You can simply call Dispose on the MailMessage. This will trigger the dispose on any attachments.

    objSMTP.Send(mail) 
    mail.Dispose()
    

    this could be avoided if you encapsulate the mail object with the Using statement

    Using(mail as MailMessage = New MailMessage())
    ....
    End Using
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Imports System.Data.SqlClient Module Module1 Sub Main() Dim iCount As Integer = 1 Try Do
Quiz question: What is the output of running the following program: Sub Main() Try
This is the code I have: Sub Main() Dim dts As New DataSet Dim
I have this algorithm that I want to implement on VB6. Sub Main() dim
I have this TreeView: Main Node Header=Main Sub Node Header=Sub1 Final Node Header=Item1 Final
I have application with this code: Module Startup <STAThread()> _ Public Sub Main() Try
Why won't XMLSerializer process my generic list? Sub Main() Serializing() End Sub <System.Serializable()> _
Currently have password protection on my main and sub directories, however I'd like to
I have main categories and sub categories. I want to be able to sort
Requirements Project should contain 1 main application and some secondary sub applications (which uses

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.