tags:

views:

126

answers:

3

I am currently doing the following:

  • I create a file using FileStream braced in the using() tag - Only the file creation takes place within the using statement. The rest of the statements are nearly sequential.
  • Launch the file I have created using Process.Start()
  • Read a small bit of meta data from the file using Assembly.ReflectionOnlyLoadFrom()
  • List the running processes using Process.GetProcessesByName
  • Kill the Process using Process.Kill
  • Try to delete the file using File.Delete()

My problem is that my application is locking the file, so when I try to delete it, nothing happens. It throws an exception saying "Access is Denied" if I try to delete, and throws "Another process is using this file" if I try to write to it.

What on earth could be consuming the file from the above (literally all there is)? I have set all references to null, and gone as far as to call the dreaded GC.Collect() and no luck.

+1  A: 

You can do an analysis with the Process Monitor tool. It can capture the call stack of each file access thus should be sufficient to tell you where/when the file is locked.

Findekano
I agree Process monitor should solve this for you - if you just need to be sure which process is locking the file, process explorer (also from sysinternals) has a find function in which you can type the filename and the locking process will be reported back.
morechilli
A: 

When you load assembly it's hosted in the current AppDomain. If you load it using ReflectionOnlyLoad(byte[]) it will load it as shadow and won't lock the file.

var bytes = File.ReadAllBytes(path);
var assembly = Assembly.ReflectionOnlyLoad(bytes);

Currently, the code that blocks the file is Assembly.ReflectionOnlyLoadFrom() and not the writing to the file (assuming the FileStream is disposed before trying to delete). The file will be released only when the AppDomain is unloaded.

Elisha
Thanks Elisha, when I use this I receive `API restriction: The assembly '' has already loaded from a different location. It cannot be loaded from a new location within the same appdomain.`
Secure The World
Has it been loaded before? Are you loading it twice or referencing it from the code?
Elisha
No, this is the first call to load the assembly. Does it matter that the assembly is running though? I started it using Process.Start(), so it shouldn't be loaded into the same app domain. I'm rather confused.
Secure The World
Is the assembly referenced from the executing assembly? The fact it's running in another process shouldn't matter.
Elisha
+1  A: 

Clearly, you are doing something in your code that causes the assembly to be loaded in your own process. Once there, it is stuck and the file is locked until the AppDomain in which it gets loaded gets unloaded. Which is no doubt the primary one, it's stuck until your program terminates.

Pay attention to the Visual Studio Output window, you'll get a debugger notification as soon as it gets loaded. Stepping through your code while watching the windwo will easily isolate the statement that causes it.

Hans Passant