Home > OS >  FileVersionInfo.GetVersionInfo not letting go of file
FileVersionInfo.GetVersionInfo not letting go of file

Time:02-24

I'm trying to check the version of the Windows application the user is running against the version that is on the server to see if the local version needs to be updated. I do this by calling

FileVersionInfo.GetVersionInfo("path to file on server") 

and reading the version information. This works fine for what I need, however, when I look at open files on the server from the computer management console, I see many instances of my file open in Read mode. This causes a problem when I need to copy a new version to the server. I first have to close all of the open files before it will let me write to it. Even though they are only open in Read mode, it still makes me close them.

Is there a better way to get the file version from the server? Or is there a way to dispose of the FileInfo variable that I am using so it disconnects?

CodePudding user response:

I have not found a good resolution using FileVersionInfo. And from my research using FileVersionInfo does not release the variable as you would expect so I changed to getting the file information from a PowerShell script. This allows me to dispose the object and release the file as I have tested successfully. So here is the code that gets the information:

Dim server_version As String = ""
Dim invoker As New RunspaceInvoke
Dim command As String = "(Get-Item path_to_file_no_quotes_needed).VersionInfo.FileVersion"

   Try
      Dim outputObjects As Collection(Of PSObject) = invoker.Invoke(command)
      For Each result As PSObject In outputObjects
        server_version = result.ToString
      Next
   Catch ex As Exception

   End Try
   invoker.dispose

When this is run from my app, I see the file get opened in Read mode on the server but it disappears after about 3 seconds. So I can see the object is being properly disposed. Just FYI, there is no FileVersionInfo.dispose method.

In addition, for this to work you need:

Imports System.Management.Automation

which needs a reference in your project. It can be found in:

C:\Windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\System.Management.Automation.dll

CodePudding user response:

In the Shown event I have code that looks like this,

Try
    If My.Application.IsNetworkDeployed AndAlso My.Application.Deployment.CheckForUpdate Then
        'update available - the update will happen when app is closed
    End If
Catch ex As Exception
    'error while checking for update or update is corrupt
End Try

I do this so I can provide the user an indication that an update is available.

In the FormClosed event I do,

Try
    'see if update is available, and install if it is
    If My.Application.IsNetworkDeployed Then
        If My.Application.Deployment.CheckForUpdate Then
            My.Application.Deployment.Update()
        End If
    End If
Catch ex As Exception
End Try
  • Related