• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 304
  • Last Modified:

Can't delete a compiled file - "file in use"

I'm running the code below that compiles and runs a dll. It works great - except when the program loops back around it can't delete the existing dll because "file is in use"
Once the dll below is created and run, how can i released it so i can then delete it?
            If m_Code.Trim <> "" Then
                If m_Namespace.Trim <> "" Then
                    If m_ClassName.Trim <> "" Then
                            If File.Exists(m_AppPath & "\" & m_Namespace & ".dll") Then File.Delete(m_AppPath & "\" & m_Namespace & ".dll")
                            If File.Exists("c:\windows\system32\" & m_Namespace & ".dll") Then File.Delete("c:\windows\system32\" & m_Namespace & ".dll")
                        End Try
                        Dim VBP As New VBCodeProvider()
                        Dim CVB As System.CodeDom.Compiler.ICodeCompiler
                        CVB = VBP.CreateCompiler
                        Dim PM As New System.CodeDom.Compiler.CompilerParameters()
                        PM.GenerateInMemory = False
                        PM.OutputAssembly = m_Namespace & ".dll"
                        PM.MainClass = m_ClassName
                        PM.IncludeDebugInformation = True
                        'Add assemblies
                        Dim ASM As System.Reflection.Assembly
                        For Each ASM In AppDomain.CurrentDomain.GetAssemblies()
                        PM.ReferencedAssemblies.Add(m_AppPath & "\ActiveReports.dll")
                        PM.ReferencedAssemblies.Add(m_AppPath & "\ActiveReports.XlsExport.dll")
                        PM.ReferencedAssemblies.Add(m_AppPath & "\ActiveReports.PdfExport.dll")
                        PM.ReferencedAssemblies.Add(m_AppPath & "\ActiveReports.Viewer.dll")
                        PM.ReferencedAssemblies.Add(m_AppPath & "\interp.business.dll")
                        'here we do the deed
                        Dim Results As System.CodeDom.Compiler.CompilerResults
                        Results = CVB.CompileAssemblyFromSource(PM, m_Code)
                        'this is just to list out all the errors if any
                        Dim Err As System.CodeDom.Compiler.CompilerError
                        For Each err In Results.Errors
                            m_Results &= "Number " & err.ErrorNumber & " Text " & err.ErrorText & " Line " & err.Line & VbCrLf
                        Dim RunObj As New Object()
                        Dim vArgs() As Object
                        RunObj = Results.CompiledAssembly.CreateInstance(m_Namespace & "." & m_ClassName, False, Reflection.BindingFlags.CreateInstance, Nothing, vArgs, Nothing, Nothing)
                        If RunObj Is Nothing Then
                            m_Results &= "Unable to run dll - failed." & VbCrLf
                        End If
                        m_Results &= "No class name found." & VbCrLf
                    End If
                    m_Results &= "No namespace found." & VbCrLf
                End If
                m_Results &= "No code found." & VbCrLf
            End If
            If Len(m_Results) = 0 Then m_Results = "OK"
        Catch err As Exception
            m_Results &= "ERROR: " & err.Message & " " & err.StackTrace & VbCrLf
        End Try

Open in new window

1 Solution
You can only unload an assembly if it is in another appdomain.
So you must create an appdomain which loads the compiled assembly and when it should unload you must unload the whole appdomain.

Here is an MSDN Magazine Article about it.
LosBearAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now