无法将类型为“ system.IO.DirectoryInfo”的对象转换为类型为“ System.IO.FileInfo”的对象

时间:2019-01-15 09:21:24

标签: mysql vb.net

以下代码中的where子句出现错误。错误状态: “无法将类型为'System.IO.DirectoryInfo'的对象强制转换为类型为'System.IO.FileInfo'

Try
    Dim query As MySqlCommand = SQLConnection.CreateCommand
    Dim dir As DirectoryInfo = New DirectoryInfo("\\10.2.1.40\IT\BACKUPDATA\MBP_DATA\")
    For Each fi As FileInfo In dir.GetFileSystemInfos
        query.CommandText = "UPDATE backup_db set tgl = @date1, ukuran = @ukuran, issaturday = @issaturday, iseom = @iseom,  pic = @pic
'                                        WHERE ukuran = 0 and tgl = @date1"
        query.Parameters.Clear()
        query.Parameters.AddWithValue("@date1", fi.LastWriteTime.ToString("yyyy-MM-dd"))
        query.Parameters.AddWithValue("@ukuran", fi.Length)
        query.Parameters.AddWithValue("@issaturday", "0")
        query.Parameters.AddWithValue("@iseom", "0")
        query.Parameters.AddWithValue("@pic", "Carles")
        query.ExecuteNonQuery()
    Next
Catch ex As Exception
    MsgBox(ex.Message, MsgBoxStyle.Information)
End Try

1 个答案:

答案 0 :(得分:1)

GetFileSystemInfos返回一个FileSystemInfo数组,而不是FileInfo

For Each fi As FileSystemInfo In dir.GetFileSystemInfos()
   ' ... '
Next

FileInfo也是FileSystemInfo,因为它是子类,但反之亦然。 DirectoryInfo.GetFileSystemInfos返回文件和目录。因此,您不能将它们强制转换为FileInfo

但是由于您需要Length,所以我假设您实际上只需要文件。然后使用GetFiles

For Each fi As FileInfo In dir.GetFiles()
   ' ... '
Next