Reflection and DirectoryInfo.Delete error

  • Thread starter Thread starter goHawkeyes
  • Start date Start date
G

goHawkeyes

After using System.Reflection.Assembly.ReflectionOnlyLoadFile to look
through a directory and inspect versions of files and such I use a
DirectoryInfo.Delete(True) object to delete the directory but it gives
me an unauthorized error. If I do everything the same way but do not
inspect the directory contents with reflection I can delete the
directory. I have tried setting my assembly variable to nothing as
well as force garbage collection yet nothing seems to fix the problem.
Is there a bug here or am I missing something?

Thanks,

AJL
 
If you are using System.Reflection.Assembly.ReflectionOnlyLoadFile to list
the contents of directory where an assembly is loaded for your application
then you are going to have to unload the assembly loaded from that directory
before attempting to delete the directory. If this is the case then why
would you want to delete the directory.

HTH

Ollie Riches
 
Here is the solution I found. I didn't realize I could not easily
unload the assembly from my appDomain. One work around is below,
sorry I couldn't find the originally group link. You basically make a
copy of the file so as not to load the originally into the appdomain.

Private Function GetDllInfo(ByVal filePath As String) As DllInfo

Dim asm As Assembly
Dim dllVersionInfo As New DllInfo

Dim stream As New FileStream(filePath, FileMode.Open,
FileAccess.Read)
Dim memstream As New MemoryStream()

Dim b(4096) As Byte

While stream.Read(b, 0, b.Length) > 0

memstream.Write(b, 0, b.Length)

End While

asm = Assembly.Load(memstream.ToArray())

dllVersionInfo.DllVersion = asm.GetName.Version.ToString
dllVersionInfo.DllName = asm.GetName.Name.ToString

memstream.Close()
stream.Close()

Return dllVersionInfo

End Function


Public Class DllInfo

Dim version As String
Dim name As String

Public Sub New()
End Sub

Public Property DllVersion() As String
Get
Return version
End Get
Set(ByVal value As String)
version = value
End Set
End Property

Public Property DllName() As String
Get
Return name
End Get
Set(ByVal value As String)
name = value
End Set
End Property
End Class
 
Back
Top