4

I have this piece of code, which loads a file into RAM, then loads it as an assembly into an AppDomain (d):

var a = d.Load(File.ReadAllBytes(tmp));

The problem is that when I later try to delete the file located at tmp (right now just using File Explorer), I get an error saying that the file is still open in my program. I even tried using the using keyword with File.Open, and that didn't seem to work either. Can someone please explain why this might be happening, and how to fix it?

Nick Mertin
  • 1,149
  • 12
  • 27
  • 2
    `File.ReadAllBytes` does not keep an open stream to the file. Perhaps it is somewhere else in your code? – Magnus Oct 20 '15 at 18:07
  • 2
    You open the file at another location. This statement is correct it uses a FilesStream which is already disposed when it returns the byte array. How do you create the temp file? – Alois Kraus Oct 20 '15 at 18:08
  • @Alois I used `Path.GetTempFilePath()` – Nick Mertin Oct 20 '15 at 18:12
  • Have you loaded this assembly in another AppDomain? Check out with Visual Studio the Loaded Modules list if your file shows up as loaded assembly. – Alois Kraus Oct 20 '15 at 18:14
  • Then I use `File.Copy` to copy an original to the new location. It's interesting, the original doesn't seem to be deletable either. – Nick Mertin Oct 20 '15 at 18:14
  • Good idea, I'll try that when I have a chance. – Nick Mertin Oct 20 '15 at 18:15
  • 2
    Probably something else is keeping the file open. If it's not your code, it might be antivirus software, especially if it's a file you've only just created. – Joe Oct 20 '15 at 18:15
  • @Joe no, it says the name of my program. What's interesting, however, is that I can rename the file just fine. – Nick Mertin Oct 20 '15 at 18:16
  • I tried to reproduce, but i had no problems with deleting the file. Please, check, that your project does not have a reference to "tmp". It is possible that CLR already loaded it. You can see the list of assemblies, loaded in .net process in Process Explorer. http://stackoverflow.com/questions/458362/how-do-i-list-all-loaded-assemblies – Dmitrii Dovgopolyi Oct 20 '15 at 18:16
  • You can rename locked files. I do it often to replace loaded dlls with new ones. – Alois Kraus Oct 20 '15 at 18:18
  • @Magnus the related links down there in the corner has a [solution](http://stackoverflow.com/questions/17838378/is-assembly-loadfrom-keeping-an-open-file-handle?rq=1) that you might want to look at. – Crowcoder Oct 20 '15 at 18:21
  • @Alois that's basically what I'm trying to do here, the main problem is that I the original (before copying) is still locked as well – Nick Mertin Oct 20 '15 at 18:21
  • [The source code](http://referencesource.microsoft.com/#mscorlib/system/io/file.cs,4b24188ee62795aa) shows a `using` block. – Uwe Keim Oct 20 '15 at 18:28
  • We really need to see how you create and write `temp` and how you communicate the filename to the delete. Write a small postable program to reproduce this if necessary. – H H Oct 20 '15 at 18:38
  • @Henk right now for testing I've just tried to delete the file using file explorer – Nick Mertin Oct 20 '15 at 18:53
  • We've established that it's not ReadAllBytes(). The problem is somewhere else, and you're not telling/showing us anything. – H H Oct 21 '15 at 07:15

1 Answers1

0

ReadAllBytes is supposed to close the file as part of its function. It sounds like something is preventing it from closing, and therefore you cannot delete. Maybe there's an exception happening that needs to be caught.

Here is a similar question with a solution which may be helpful: Unable to delete some files via System.IO.File in C# console app

Community
  • 1
  • 1
Smurfie
  • 124
  • 5