Tuesday, February 07, 2012
File Properties using VB.NET leave file locked
Last Post 30 Aug 2010 09:57 AM by Bizare. 2 Replies.
AddThis - Bookmarking and Sharing Button Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
BizareUser is Offline
New Member
New Member
Posts:11

--
27 Aug 2010 04:36 PM  

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

JRUser is Offline
New Member
New Member
Posts:91

--
30 Aug 2010 03:20 AM  

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.

BizareUser is Offline
New Member
New Member
Posts:11

--
30 Aug 2010 09:57 AM  
Yes! This works like a charm. Thanks for the expert advice.
You are not authorized to post a reply.

Active Forums 4.2
Copyright © 2011 JasonNewell.NET