Solved

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

Posted on 2008-10-23
2
284 Views
Last Modified: 2012-05-05
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?
Try

            If m_Code.Trim <> "" Then

                If m_Namespace.Trim <> "" Then

                    If m_ClassName.Trim <> "" Then

 

                        Try

                            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")

                        Catch

                        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(ASM.Location)

                        Next

                        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

                        Next

 

                        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

 

                    Else

                        m_Results &= "No class name found." & VbCrLf

                    End If

                Else

                    m_Results &= "No namespace found." & VbCrLf

                End If

            Else

                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

0
Comment
Question by:LosBear
2 Comments
 
LVL 11

Accepted Solution

by:
melmers earned 250 total points
ID: 22892996
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.
http://msdn.microsoft.com/en-us/magazine/cc163476.aspx
0
 
LVL 1

Author Closing Comment

by:LosBear
ID: 31509268
Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tabcontrol Caption? 1 31
VB.NET bulk update  of a table from another table 4 36
vb.net 2 37
VB.Net - Windows UserName index and length error 4 22
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now