File Properties using VB.NET leave file locked

 

I am converting code from VB6 to VB.net whose purpose is to access file properties.  In VB6, it works just fine.

But in VB.NET, the propertysets.CLOSE() function does not seem to release the file.  At any rate, the SECOND time

 I call the function (on the same file), it fails at the point indicated below.  Once the program exits, the file

 is released.  Then I can re-run the code, calling pTEST successfully the first time, but not the second time.

Any ideas how to make vb.net reliable access file properties?

SE version 20

I call pTEST like this:

sTest = pTEST("C:\drawingfiles\amaxdeck\A20000015.asm", "Title", "SummaryInformation") 'succeeds first time

sTest = pTEST("C:\drawingfiles\amaxdeck\A20000015.asm", "Title", "SummaryInformation") 'vb6 OK; vb.NET fails

 

*** VB6 version *** work fine every time.

Public Function pTEST(ByRef FileWithProp As String, ByRef pKey As String, ByRef pGroup As String) As String

    'Solid Edge V20  VB6

 

    'FileWithProp = "C:\drawingfiles\amaxdeck\A20000015.asm" pKey = "Title" pGroup = "SummaryInformation"

    Dim fPropSets As SolidEdgeFileProperties.PropertySets       'PropertySets - for files not open

    Dim fProp As Object             'Properties - for files not open

    Dim ObjProp As Object       'SolidEdgeFramework.Properties

    Dim sPropTest As String

    Set fPropSets = CreateObject("SolidEdge.FileProperties")

    fPropSets.Open FileWithProp

    Set fProp = fPropSets(pGroup)

    pTEST = fProp(pKey).value

 

    If Not (fProp Is Nothing) Then

        Set fProp = Nothing

    End If

    If Not (fPropSets Is Nothing) Then

        fPropSets.Close

        Set fPropSets = Nothing

    End If

End Function

 

*** vb.net version *** works right the first time, but fails the second time called for the same file.

   Public Function pTEST(ByRef FileWithProp As String, ByRef pKey As String, ByRef pGroup As String) As String

        'return the fileproperty pKey in group pGroup in file FileWithProp

        'FileWithProp = "C:\drawingfiles\amaxdeck\A20000015.asm"

        ' pKey = "Title" pGroup  = "SummaryInformation"

        'SolidEdge V20  vb.net 2010 Express

        Dim fPropSets As Object ' SolidEdgeFileProperties.PropertySets       

        Dim fProp As Object             'Properties - for files not open

        Dim sPropTest As String = ""

        fPropSets = CreateObject("SolidEdge.FileProperties")

        fPropSets.Open(FileWithProp)        'code fails here the second time called with the same file!!!!!!!

        fProp = fPropSets(pGroup)

        pTEST = fProp(pKey).value

        If Not (fProp Is Nothing) Then

            Runtime.InteropServices.Marshal.ReleaseComObject(fProp)

            fProp = Nothing

        End If

        If Not (fPropSets Is Nothing) Then

            fPropSets.Close()                   'This seems like it should close and release the file, but it does not!!!

            Runtime.InteropServices.Marshal.ReleaseComObject(fPropSets)

            fPropSets = Nothing

        End If

    End Function

 

Posted by: Jon Zaerr
Post date: 8/27/2010 11:36:12 AM

2 REPLIES

RE: File Properties using VB.NET leave file locked

 

after reading the properties you must ever start Garbage Collection: 

   .....

  GC.Collect()

  GC.WaitForPendingFinalizers()

  GC.Collect()

  GC.WaitForPendingFinalizers()

End Function

 

The GC needs to be called twice in order to get the finalizers called -

the first time: it simply makes a list of what is to be finalized,

the second time: it actually the finalizing.

Only then will the object do it automatic ReleaseComObject.

 

Posted by: JR
Post date: 8/29/2010 10:20:06 PM

RE: File Properties using VB.NET leave file locked

Yes! This works like a charm. Thanks for the expert advice.

 

Posted by: Jon Zaerr
Post date: 8/30/2010 4:57:18 AM