-1

I´ve written some code to send files as attachments of .eml files with C#.
I create the files in an Windows temp folder and attach them to a .eml file. The .eml files are simply opened and send through Process.Start(filename); with the standard mailing program in windows. If the users wishes to send the file again, the file will be rewritten from the database to make sure its the newest version.

Now to my problem: Randomly a few times or after just the first time I rewrite the file the error appears

The process cannot access the file because it is being used by another process

I acutally did some research on the web and found this other question.
So I made sure like mentioned there, that the FileStream is set correctly with all arguments.
var fs = new FileStream(tempPathSave, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)
Has someone an idea what is the reason for my problem? My biggest problem is, that I can´t really duplicate it to say it is happening when I do this or that.

EDIT: I am using the FileStream in an using statement to make sure that it is disposed right.

Only3lue
  • 245
  • 1
  • 3
  • 20
  • 2
    In the question you are referring to, the FileStream is inside the 'using' block so it can be disposed properly. – EylM Aug 24 '20 at 14:41
  • Is it possible that the email program still has the file in access? – Fildor Aug 24 '20 at 14:42
  • I have everything in an using statement to make sure that the FileStream is closed. I checked always the Task Manager that the mailing pogram is closed. But I can have the mailing program open a few times, too until the exception appears. Sometimes it appears directly after the first time. So...I have no idea. – Only3lue Aug 24 '20 at 14:44
  • 2
    Well, a workaround would be to just use another filename ... the FS can be a b1tch sometimes. – Fildor Aug 24 '20 at 14:48
  • Yes my idea was to prevent the temp folder to be flooded with files. – Only3lue Aug 24 '20 at 14:49
  • 2
    I agree with @Fildor 's concept. I would create a dedicated directory in the temp folder and use unique file names (guid). Periodically, I will do a clean up - delete files older than let's say 2 hours. – EylM Aug 24 '20 at 14:55
  • What is throwing that error? The mail program? Your own? If so, at which line? – Alejandro Aug 24 '20 at 14:58
  • I get that you don't want to flood the temp folder with files, but I guess getting the mail out is more important than not using another file at that moment. So, that out of the way, I'd have thorough cleanup mechanisms _**or**_ use MailKit + SQLite instead of the default mail program and filesystem. – Fildor Aug 24 '20 at 15:05
  • The program itself throws the exception at the point it trys to rewrite the file. But for now it seems that I have to try out the solution with always creating a new file. The other thing is, that meanwhile in lets say 2 hours the user could delete the file and upload an other file with the same name. – Only3lue Aug 24 '20 at 20:50

2 Answers2

2

It is hard to properly understand what the problem is without actually seeing the code but from my experience you are not properly disposing the filestream object so it keeps a handle to the file

var fs = new FileStream(tempPathSave, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)

So when you are done using your filestream, call

fs.Dispose();

which will dispose your filestream properly.

Moreover, you can use syntatic sugar, using which will automatically dispose your object

using(var fs = new FileStream(tempPathSave, FileMode.Open, FileAccess.Write, FileShare.ReadWrite)){
// use fs here
// STUFF
} //at the end fs will be disposed
Hasan Emrah Süngü
  • 3,488
  • 1
  • 15
  • 33
  • While your suggestion is good practice, in this case, it could also be the mail program that does not cleanly release file handles. The windows fs _can_ be quite bitchy about all this. – Fildor Aug 24 '20 at 15:02
  • Actually I am using the FileStream in an using condition...so...disposing the FS should not the problem. – Only3lue Aug 24 '20 at 20:51
0

I´ve found a solution for my problem. I just changed the FileStream to MemoryStream because the MailMessage constructor can handle a stream, too. The only point there is, that you have to keep the MemoryStream open until the .eml file is saved. And more important you have to watch out about the memory usage und make sure the afterwards every MemoryStream will be disposed.

Only3lue
  • 245
  • 1
  • 3
  • 20