I have an app which is opening a series of DLLs on the filesystem and in the GAC, and which is holding a lock on those files. How can I release these handles explicitly so I can rebuild them in Visual Studio without closing my app?
Code follows:
private void BuildTabPage_AssemblyTree(string filename, string foldername)
{
Assembly assembly;
try
{
assembly = Assembly.LoadFrom(filename);
}
catch (Exception ex)
{
MessageBox.Show("Error loading assembly " + filename + "\n" + ex.Message);
return;
}
TreeNode tRoot = BuildNode(assembly, foldername);
tvAssemblies.Nodes.Add(tRoot);
tvAssemblies.ExpandAll();
txtResults.Text =
RefsFound.ToString() + " References Located in Filesystem\r\n" +
RefsInFramework.ToString() + " References Located in Framework\r\n" +
RefsInGac.ToString() + " References Located in GAC\r\n" +
RefsNotFound.ToString() + " References Not Found: \r\n\r\n";
foreach (string s in MissingFiles)
txtResults.Text += s + "\r\n";
}
private TreeNode BuildNode(Assembly assembly, string foldername)
{
TreeNode tn = new TreeNode(assembly.GetName().Name);
tn.ToolTipText = assembly.FullName + "\n" + assembly.CodeBase;
AssemblyName[] assemblies = assembly.GetReferencedAssemblies();
foreach (AssemblyName a in assemblies)
{
string filename2 = foldername + a.Name + ".dll";
TreeNode tn2;
if (System.IO.File.Exists(filename2))
{ // File found in folder
RefsFound++;
Assembly assy = Assembly.LoadFile(filename2);
tn2 = BuildNode(assy, foldername);
}
else if (a.Name.StartsWith("System"))
{ // Framework assemblies not included
RefsInFramework++;
tn2 = new TreeNode();
tn2.Text = a.Name;
tn2.ForeColor = System.Drawing.Color.Green;
tn2.ToolTipText += "\n.NET Framework File";
}
else
{
try
{ // Find file in GAC
Assembly assy = Assembly.Load(a);
tn2 = new TreeNode(a.Name);
tn.ToolTipText = assembly.FullName + "\n" + assembly.CodeBase + "\nFile detected in GAC";
tn2.Text = "(" + filename2.Substring(filename2.LastIndexOf("\\") + 1) + ")";
tn2.ForeColor = System.Drawing.Color.Green;
RefsInGac++;
}
catch (System.IO.FileNotFoundException)
{ // File not Found
RefsNotFound++;
if (!MissingFiles.Contains(a.Name))
MissingFiles.Add(a.Name);
tn2 = new TreeNode();
tn2.Text = "(" + filename2.Substring(filename2.LastIndexOf("\\") + 1) + ")";
tn2.ToolTipText = "File Not Found";
tn2.ForeColor = System.Drawing.Color.Red;
}
}
tn.Nodes.Add(tn2);
}
return tn;
}