Searching a directory and file locking.

  • Thread starter Thread starter Scott Meddows
  • Start date Start date
S

Scott Meddows

I'm using a VB webservice to create the back end of a smart client
application. When the service loops through the directory containing all
the assemblies the application can load it seems to put a file lock on the
assemblies. Is there a way to prevent this? Posted below is the code.

Thanks

Dim f As FileInfo
Dim d As New DirectoryInfo(searchpath)

For Each f In d.GetFileSystemInfos("*.dll")

Try

Dim currentAsm As [Assembly] = [Assembly].LoadFrom(f.FullName)

Dim temptype As Type

For Each temptype In currentAsm.GetExportedTypes()
If (temptype.IsSubclassOf(basetype)) And Not
IsSubForm(temptype) Then

Dim fi As New FormInfo.FormInfo

Dim myhandler As EventHandler

fi.Name = GetName(temptype)
fi.Description = GetDescription(temptype)
fi.AssemblyName = temptype.[Module].Name
fi.TypeName = temptype.FullName
fi.UrlLocation = urlLocation
fi.ImageFile = GetImageFileName(temptype)

forms.Add(fi)
End If

Next temptype


Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try

Next f
d = Nothing
 
Assemblies cannot be unloaded independently because there could be code in
other assemblies that uses types from the assembly that you want to unload.
However you can unload AppDomain and this will unload all assemblies that
are loaded there. If you want to be able to unlock assembly files you need
to load them in a separate AppDomain and when done unload the whole domain.
Make sure that no types cross the domain boundary. If a type gets used in a
different domain it will cause the assembly where the type is declared to
be loaded in that domain as well.

More information on working with AppDomains could be found here:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemappdomainclasstopic.asp

Vladimir [VB.Net team]


--------------------
| Reply-To: "Scott Meddows" <[email protected]>
| From: "Scott Meddows" <[email protected]>
| Subject: Searching a directory and file locking.
| Date: Mon, 29 Sep 2003 09:13:19 -0500
| Lines: 47
| Organization: The Schumacher Group
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
| Message-ID: <#[email protected]>
| Newsgroups: microsoft.public.dotnet.languages.vb
| NNTP-Posting-Host: 209.215.85.43
| Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.languages.vb:142177
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| I'm using a VB webservice to create the back end of a smart client
| application. When the service loops through the directory containing all
| the assemblies the application can load it seems to put a file lock on the
| assemblies. Is there a way to prevent this? Posted below is the code.
|
| Thanks
|
| Dim f As FileInfo
| Dim d As New DirectoryInfo(searchpath)
|
| For Each f In d.GetFileSystemInfos("*.dll")
|
| Try
|
| Dim currentAsm As [Assembly] = [Assembly].LoadFrom(f.FullName)
|
| Dim temptype As Type
|
| For Each temptype In currentAsm.GetExportedTypes()
| If (temptype.IsSubclassOf(basetype)) And Not
| IsSubForm(temptype) Then
|
| Dim fi As New FormInfo.FormInfo
|
| Dim myhandler As EventHandler
|
| fi.Name = GetName(temptype)
| fi.Description = GetDescription(temptype)
| fi.AssemblyName = temptype.[Module].Name
| fi.TypeName = temptype.FullName
| fi.UrlLocation = urlLocation
| fi.ImageFile = GetImageFileName(temptype)
|
| forms.Add(fi)
| End If
|
| Next temptype
|
|
| Catch ex As Exception
| Console.WriteLine(ex.ToString())
| End Try
|
| Next f
| d = Nothing
|
|
|
 
Back
Top