I am stuck for 3rd day with this problem and I have no freaking idea why this doesn't work. I just want to load external .dll to read some info using reflection and delete the file after all. The problem is that read files are locked. The most strange thing is that only two files are locked while i read 5 of them succesfully. I've tried ShadowCopy with no result. I got no clue right now.
This is my appdomain class:
public class AppDomainExpander
{
private Type[] _types;
public Type[] Types
{
get { return _types; }
set { _types = value; }
}
public void Create(string domainName, string path)
{
AppDomainSetup aps = new AppDomainSetup();
aps.ShadowCopyFiles = "true";
AppDomain dmn = AppDomain.CreateDomain(domainName);
string typename = typeof(DomainCommunicator).FullName;
string assemblyName = typeof(DomainCommunicator).Assembly.FullName;
var inner = (DomainCommunicator)dmn.CreateInstanceAndUnwrap(assemblyName, typename);
inner.Create();
Assembly assembly = Assembly.LoadFrom(path);
Types = assembly.GetTypes();
AppDomain.Unload(dmn); //it's strange that the code even work because i try to unload domain before i get Types[]
}
public class DomainCommunicator : MarshalByRefObject
{
public void Create()
{
AppDomain.CurrentDomain.DomainUnload += new EventHandler(OnDomainUnload);
}
void OnDomainUnload(object sender, EventArgs e)
{
AppDomain.CurrentDomain.DomainUnload -= new EventHandler(OnDomainUnload);
}
}
}
And this is how i try to use it:
var expander = new AppDomainExpander();
expander.Create("MyDomain", file.Path);
foreach (var type in expander.Types)