FileStream does not close file

I have the following code:

using (MemoryStream str = new MemoryStream()) { Program.api.GetDocument(result, str); using (FileStream fileStream = File.Create(filePath)) { str.WriteTo(fileStream); } } 

Whenever a file is written, it is always blocked subsequently - an attempt to delete or modify it causes Windows to tell me that the file is being used, even after closing my application. Did I miss something?

+8
c # filestream memorystream
source share
1 answer

Your problem is most likely caused by the indexing of Windows Search, which is part of Windows Search . If you try to access the file immediately (or very soon) after changing it, you may encounter the problems that you see. The best thing is to add retry logic to your file operation, which waits a short period of time and retries the op file.

If you want to confirm that the problem is caused by indexing the Windows file search, you can disable it for the file type and / or location where you are writing the file to see if the problem goes away.

+1
source share

All Articles