暴雨前的宁静

博客园 首页 新随笔 联系 订阅 管理

今天做了个读取文件信息的小例子,发上来给大家看看,有什么不对的提醒我一下啊。

首先添加个VB的类库

Imports EdmLib
Namespace FilesInfo
    Public Class FilesInfo
        Dim vault As IEdmVault7
        Dim lst As IEdmBatchListing2
        Dim cols() As EdmListCol
        Dim folder As IEdmFolder5
        ''' <summary>
        ''' 构造函数
        ''' </summary>
        ''' <param name="path">需要读取的文件目录名</param>
        ''' <param name="mvault">电子仓库</param>
        ''' <remarks>只读取已SLd为扩展名开头的文件</remarks>
        Public Sub New(ByVal path As String, ByVal mvault As IEdmVault7)
            vault = mvault    '为vault赋值
            lst = vault.CreateUtility(EdmUtility.EdmUtil_BatchList) '创建 IEdmBatchListing2对象

            If path = "" Then  '如果没有添加路径则退出程序
                Exit Sub
            End If
            folder = vault.GetFolderFromPath(path) '获取vault指定的文件夹
            Dim pos As IEdmPos5 = folder.GetFirstFilePosition() '创建pos对象用于遍历文件夹中的文件
            While Not pos.IsNull
                Dim file As IEdmFile5 = folder.GetNextFile(pos) '创建file对象并赋值
                AddFile(file.GetLocalPath(folder.ID), 1)
            End While
        End Sub
        ''' <summary>
        ''' 向IEdmBatchListing2加入文件
        ''' </summary>
        ''' <param name="filename">文件名</param>
        ''' <param name="lparam">通常为1</param>
        ''' <remarks></remarks>
        Public Sub AddFile(ByVal filename As String, Optional ByVal lparam As Integer = 1)
            Dim ft As Date
            Try
                ft = FileSystem.FileDateTime(filename)      '取得文件夹中文件的日期
            Catch
                MsgBox(vault.GetErrorMessaage(Err.Number))
            Finally
                lst.AddFile(filename, ft, lparam)           '向IEdmBatchListing2加入文件
            End Try
        End Sub
        ''' <summary>
        ''' 获取零件信息
        ''' </summary>
        ''' <param name="SetName">Bom条目名</param>
        ''' <returns>List(Of Dictionary(Of String, String))</returns>
        ''' <remarks></remarks>
        Function GetFilesInfo(ByVal SetName As String) As List(Of Dictionary(Of String, String))
            Dim infolst As List(Of Dictionary(Of String, String)) = New List(Of Dictionary(Of String, String))() '定义一个泛型数组
            Dim files() As EdmListFile = Nothing  ' 定义一个EdmListFile对象并赋空值,如果这里不赋空值有时会出错
            GetFiles(files, SetName)  '获取文件夹中的文件
            For Each efile As EdmListFile In files         '循环添加List中的文件到一维数组infodic中
                Dim infodic As Dictionary(Of String, String) = New Dictionary(Of String, String) '定义一个一维的泛型数组
                infodic.Add("Version", efile.mbsRevisionName)    '添加版本号
                infodic.Add("State", efile.moCurrentState.mbsStateName) '添加状态
                infodic.Add("FileID", efile.mlFileID.ToString())       '添加FileID
                infodic.Add("FolderID", efile.mlFolderID.ToString())   '添加FolderID
                Dim datas() As Object
                datas = efile.moColumnData                      '把一列赋给datas
                Dim idx As Integer = 0
                For Each col As EdmListCol In cols              '循环加列到数组

                    If Not (datas(idx) Is Nothing) Then
                        infodic.Add(col.mbsCaption, datas(idx).ToString())
                    Else
                        infodic.Add(col.mbsCaption, "")
                    End If
                    idx = idx + 1
                Next
                infolst.Add(infodic)
            Next
            GetFilesInfo = infolst                            '返回infolist
        End Function
        ''' <summary>
        ''' 获取文件
        ''' </summary>
        ''' <param name="files">添加后的所有文件</param>
        ''' <param name="SetName">BOM条目名</param>
        ''' <remarks></remarks>
        Public Sub GetFiles(ByRef files() As EdmListFile, ByVal SetName As String)
            lst.CreateListEx(SetName, 0, cols)  '调用IEdmBatchListing2中的方法,添加文件
            lst.GetFiles(files)  '获取files中的所有文件
        End Sub
    End Class
End Namespace
然后把类库编译成dll 再创建一个窗体 在窗体中添加已编译好的dll

在窗体上添加2个按钮,一个文本框,一个listview

文本框赋初值D:\Trainning\Trainning\A390  第一个Trainning是D盘的文件夹,第二个是本地试图的名字。A390是本地试图里面的文件夹

'窗体的主要代码
Imports EdmLib
Imports YTZhaoyang.Trainning.FilesInfo      '自己写的类库文件

Public Class frmTestBatchList
    Dim vault As IEdmVault7
    Dim Finfo As FilesInfo.FilesInfo
    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        vault = New EdmVault5
    End Sub


    Private Sub btn_InsertFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_InsertFiles.Click
        Finfo = New FilesInfo.FilesInfo(filePath.Text, vault)  '创建FilesInfo对象,并读取文件数据
    End Sub

    Private Sub frmTestBatchList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        vault.LoginAuto("Trainning", Me.Handle.ToInt32)    '登陆PDM

        Dim userMgr As IEdmUserMgr5        '定义UserMgr5类型对象
        userMgr = vault
        Dim pos As IEdmPos5
        pos = userMgr.GetFirstUserPosition
        Dim user As IEdmUser5
        While Not pos.IsNull
            user = userMgr.GetNextUser(pos)
            ComboBox1.Items.Add(user.Name)
        End While
    End Sub

    Private Sub btn_GetInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_GetInfo.Click
        Dim lst As List(Of Dictionary(Of String, String)) = Finfo.GetFilesInfo("零件信息")
        For Each dic As Dictionary(Of String, String) In lst
            Dim item As ListViewItem = ListView1.Items.Add(dic("项目类型"))
            For Each key As String In dic.Keys
                item.SubItems.Add(dic(key))
            Next
        Next
    End Sub
End Class
运行结果如图
首先点击读取文件,然后点击获取信息
posted on 2010-03-23 16:54  暴雨前的宁静  阅读(1613)  评论(0编辑  收藏  举报