Traverse Files and Folders in Vault Example (VB.NET)
This example shows how to recursively traverse all of the folders and files
in a vault. The paths and the names of any files checked out are
printed to the Immediate window.
NOTE: If using the .NET Framework 4.0 primary interop assembly
provided with SOLIDWORKS Enterprise PDM, see
Using .NET Framework 4.0 in
Stand-alone Applications.
'----------------------------------------------------------------------------
' Preconditions:
' 1. Start Microsoft Visual Studio 2010.
' 2. Click File > New > Project > Visual Basic > Windows Forms Application.
' 3. Type TraverseFilesFolders in Name.
' 4. Click the Browse button and browse to the folder where to create the project.
' 5. Click OK.
' 6. Create a form similar to the form shown above and change:
' a. Label to VaultsLabel.
' b. Combo box to VaultsComboBox.
' c. Button to TraverseFoldersButton.
' 7. Click View > Code.
' 8. Replace the code in Form1.vb with this code.
' 9. Replace the code in Form1.Designer.vb with this code.
' 10. Add EPDM.Interop.epdm.dll as a reference (right-click the project
' name in the Solution Explorer, Select Add Reference, Select
' Framework in the left-side panel, browse to the top folder of your
' SOLIDWORKS Enterprise PDM installation, locate and Select
' EPDM.Interop.epdm.dll, click Open, click Add, and click Close).
' 11. Right-click EPDM.Interop.epdm in References, Select Properties, and set
' Embed Interop Types to False to handle methods that pass arrays of
' structures.
' 12. Switch back to the Form1.vb code window.
' 13. Open the Immediate window.
' 14. Click Debug > Start Debugging or press F5.
'
' Postconditions:
' 1. Displays a dialog.
' 2. Select a vault.
' 3. Click the Log in and display file information button.
' 4. Examine the Immediate window.
' 5. Close the dialog.
'----------------------------------------------------------------------------
'Form1.vb
Imports EPDM.Interop.epdm
Public Class TraverseFilesFolders
Private Sub TraverseFilesFolders_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
Try
Dim vault As IEdmVault8 = New EdmVault5
Dim Views() As EdmViewInfo = Nothing
vault.GetVaultViews(Views, False)
VaultsComboBox.Items.Clear()
For Each View As EdmViewInfo In Views
VaultsComboBox.Items.Add(View.mbsVaultName)
Next
If VaultsComboBox.Items.Count > 0 Then
VaultsComboBox.Text = VaultsComboBox.Items(0)
End If
Catch ex As Runtime.InteropServices.COMException
MessageBox.Show("HRESULT = 0x" + _
ex.ErrorCode.ToString("X") + vbCrLf + _
ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub TraverseFoldersButton_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles TraverseFoldersButton.Click
Try
'Declare and create an instance of IEdmVault5 object
Dim vault As IEdmVault5 = New EdmVault5()
'Log into selected vault as the current user
vault.LoginAuto(VaultsComboBox.Text, _
Me.Handle.ToInt32())
Debug.Print(vbCrLf + "Checked out files:" + vbCrLf)
TraverseFolder(vault.RootFolder)
Catch ex As Runtime.InteropServices.COMException
MessageBox.Show("HRESULT = 0x" + _
ex.ErrorCode.ToString("X") + vbCrLf + _
ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub TraverseFolder( _
ByVal CurFolder As IEdmFolder5)
Try
'Enumerate the files in the folder
Dim FilePos As IEdmPos5
FilePos = CurFolder.GetFirstFilePosition
Dim file As IEdmFile5
While Not FilePos.IsNull
file = CurFolder.GetNextFile(FilePos)
'Get its checked out status
If file.IsLocked Then
Debug.Print(file.LockPath)
End If
End While
'Enumerate the subfolders in the folder
Dim FolderPos As IEdmPos5
FolderPos = CurFolder.GetFirstSubFolderPosition
While Not FolderPos.IsNull
Dim SubFolder As IEdmFolder5
SubFolder = CurFolder.GetNextSubFolder _
(FolderPos)
TraverseFolder(SubFolder)
End While
Catch ex As Runtime.InteropServices.COMException
MessageBox.Show("HRESULT = 0x" + _
ex.ErrorCode.ToString("X") + vbCrLf + _
ex.Message)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
End Class
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class TraverseFilesFolders
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.VaultsComboBox = New System.Windows.Forms.ComboBox()
Me.TraverseFoldersButton = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(79, 34)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(91, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Select vault view:"
'
'VaultsComboBox
'
Me.VaultsComboBox.FormattingEnabled = True
Me.VaultsComboBox.Location = New System.Drawing.Point(64, 75)
Me.VaultsComboBox.Name = "VaultsComboBox"
Me.VaultsComboBox.Size = New System.Drawing.Size(121, 21)
Me.VaultsComboBox.TabIndex = 1
'
'TraverseFoldersButton
'
Me.TraverseFoldersButton.Location = New System.Drawing.Point(64, 129)
Me.TraverseFoldersButton.Name = "TraverseFoldersButton"
Me.TraverseFoldersButton.Size = New System.Drawing.Size(121, 47)
Me.TraverseFoldersButton.TabIndex = 2
Me.TraverseFoldersButton.Text = "Log in and display file information"
Me.TraverseFoldersButton.UseVisualStyleBackColor = True
'
'TraverseFilesFolders
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(258, 201)
Me.Controls.Add(Me.TraverseFoldersButton)
Me.Controls.Add(Me.VaultsComboBox)
Me.Controls.Add(Me.Label1)
Me.Name = "TraverseFilesFolders"
Me.Text = "Traverse Folders and Files"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents VaultsComboBox As System.Windows.Forms.ComboBox
Friend WithEvents TraverseFoldersButton As System.Windows.Forms.Button
End Class