Releasing a file

J

John Wright

During my program I load an exe file using reflection. My program loads the
file using reflection, checks the assembly version and does an update if the
network version is different from the local version. I have a problem,
however, when I do the first check using reflection to get the network
version, then it locks the file and I can't delete it. How long will the
program maintain a lock on the file using reflection? I am setting the
local application to nothing to release references but it does not release
the file. See Code. Thanks.

John Wright

Public Function CheckVersion() As Boolean

Dim strLocalVersion() As String

Dim strNetworkVersion() As String

Dim asmLocal As Assembly

Dim asmNetwork As Assembly

Try

'First check to make sure the paths are set, if not exit

If Trim(Len(strLocalAsm)) = 0 Or Trim(Len(strNetworkAsm)) = 0 Then

CheckVersion = False

Exit Function

End If

'check to see if the local version is present. If not copy the exe to the
localversion location

If File.Exists(strLocalAsm) = True Then

'Connect to the local and network version and check versions

'asmLocal = Assembly.LoadFile(strLocalAsm)

asmLocal = Assembly.ReflectionOnlyLoadFrom(strLocalAsm)

'asmNetwork = Assembly.LoadFile(strNetworkAsm)

asmNetwork = Assembly.LoadFile(strNetworkAsm)

strLocalVersion = Split(asmLocal.FullName, ",")

strNetworkVersion = Split(asmNetwork.FullName, ",")

'compare the versions

If Trim(strLocalVersion(1).ToString) <> Trim(strNetworkVersion(1).ToString)
Then

'drop the references to the files

asmLocal = Nothing

asmNetwork = Nothing

'update the local file

UpdateVersion()

CheckVersion = True

Else

CheckVersion = True

End If

Else

'the file does not exsist. just copy the network file

asmLocal = Nothing

asmNetwork = Nothing

UpdateVersion()

CheckVersion = True

End If

Catch ex As Exception

asmLocal = Nothing

asmNetwork = Nothing

objError.ReportError(ex, "VersionCheck.CheckVersion", 3)

CheckVersion = False

End Try

End Function



Private Sub UpdateVersion()

'TODO: Check to see if the folder exists first. Create it if not there

Try

strDirectory = Path.GetDirectoryName(strLocalAsm)

strFileName = Path.GetFileNameWithoutExtension(strLocalAsm)

'check to see if the directory exists, if not create the directory first

If System.IO.Directory.Exists(strDirectory) = False Then

Directory.CreateDirectory(strDirectory)

End If

'TODO: Check for usage and force close

file.Copy(strNetworkAsm, strLocalAsm, True) --This line give me a
permission error file in use.

Catch ex As FileNotFoundException

objError.ReportError(ex, "VersionCheck.UpdateVersion", 5, True, True)

Catch ex As System.IO.IOException

'File is in use, rename it and copy over the new version

file.Move(strLocalAsm, strDirectory & "\" & strFileName & ".old")

File.Copy(strNetworkAsm, strLocalAsm, True)

Catch ex As Exception

objError.ReportError(ex, "VersionCheck.UpdateVersion", 4, True, True)

End Try

End Sub
 
L

Ludwig

During my program I load an exe file using reflection. My program loads the
file using reflection, checks the assembly version and does an update if the
network version is different from the local version. I have a problem,
however, when I do the first check using reflection to get the network
version, then it locks the file and I can't delete it. How long will the
program maintain a lock on the file using reflection? I am setting the
local application to nothing to release references but it does not release
the file. See Code. Thanks.

John Wright

You need to load the assembly in a new application domain. It will
then make a copy (if you set ShadowCopyFiles to true) of the original
file and use that instead of locking the original file. If you're done
you can unload the application domain.

See
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp05162002.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top