0

I need to write the contents of a listbox to a textfile which then gets added as an attachment to an email. all of this works, but only once. if i try to send it, then edit the email address it sends to, then try to rewrite and send the file it gives me this error. "the process cannot access the file because it is being used by another process".

ReceiptWriter = IO.File.CreateText(filelocation)

            For i = 0 To Me.lstRecieptBox.Items.Count - 1


                Me.lstRecieptBox.SetSelected(i, True)

                ReceiptWriter.WriteLine(Me.lstRecieptBox.SelectedItem)

            Next
            ReceiptWriter.WriteLine("-------------------")
            ReceiptWriter.Close()


I want to be able to write the information to the file and then be able to edit the email address it sends to, and it be able to write over that file and send it again.

Jxmxsyy
  • 1
  • 1
  • 4
  • It could be that it is the email process which is still using the file. You could instead use a memorystream for the attachment: [Attach a file from MemoryStream to a MailMessage in C#](https://stackoverflow.com/q/5336239/1115360) (some translation from C# will be required, but the principle remains the same). Use as many memorystreams as required. – Andrew Morton Apr 21 '19 at 18:53
  • Oh, and you should call `.Dispose()` on the MailMessage when you've finished with it, and also on the SmtpClient. – Andrew Morton Apr 22 '19 at 15:51

0 Answers0