I have a stream of byte[] which I write to a temporary file and then I send it to another method which attaches it to an email. I then want to delete the temporary folder. The code snippet I am using is as follows.
byte[] blackboxBytes = Convert.FromBase64String(backBoxBase64);
uniqueTempFolder = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()));
zipFilePath = Path.Combine(uniqueTempFolder.FullName, "BlackBox.zip");
File.WriteAllBytes(zipFilePath, blackboxBytes);
sendEmail (deviceFQN, message, ZipFilePath);
s_Log.Warn("Email sent");
//recursive delete of the whole folder
uniqueTempFolder.Delete(true);
s_Log.Warn("In BB zipFilePath after delete");
When I run, the email is getting sent and I get the log "Email sent". but after that I get an error message and the temporary directory is not deleted.
IOError: [Errno 32] The process cannot access the file 'BlackBox.zip' because it is being used by another process.
I am deleting the directory only after the email method finishes processing. So I don't know why the folder is still being processed. Any pointers will be greatly appreciated.
Also I have no access to the sendEmail method, so how can I solve this....can I probably put my code in a synchronous block or something
The retun type of sendEmail is void...I cannot modify sendEmail , but I see it has a lock when it sends the email(dispatchEmailTask).......
lock (m_QueueLock) { m_DispatchEmailTasks.Enqueue (dispatchEmailTask);}
so in my code, how can I wait for it to complete before I delete the file?