将FileInfo转换为FileSystemInfo



我正在修改文件复制程序,以前只复制一个目录到另一个目录。现在我想把目录和文件都传递到一个列表中,以便运行副本。

    For Each _fromPath In CopyList
        Dim FSinfo As FileSystemInfo()
        If Directory.Exists(_fromPath) Then
            Dim dir As New DirectoryInfo(_fromPath)
            FSinfo = dir.GetFileSystemInfos
        ElseIf File.Exists(_fromPath) Then
            Dim file As New FileInfo(_fromPath)
            FSinfo = file
        End If
        CountFiles(FSinfo)
    Next
    Private Function CountFiles(ByVal FSInfo As FileSystemInfo()) As Boolean

这段代码可以很好地用于目录。为了向CountFiles传递路径,它们必须在FileSystemInfo中。是否有方法将FileInfo转换为FileSystemInfo。

我试了试CType(file, FileSystemInfo),结果显示是Value of type 'System.IO.FileInfo' cannot be converted to '1-dimensional array of System.IO.FileSystemInfo'.

一个问题是,您正试图将对象(FileInfo)转换为对象数组(FileSystemInfo()),这是错误消息试图告诉您的。您可以编写一个重载函数来处理其中的一些问题:

' my Count functions return numeric values:
Private Function CountFiles(FSInfo As FileSystemInfo()) As Integer
Private Function CountFiles(FInfo As FileInfo) As Integer

在它们内部,FileSystemInfo可能在循环中调用另一个以避免重复的代码,或者它们都调用一个共同的过程,这取决于它是如何编写的。

最新更新