我需要检查文件(扩展名未知)是否是有效的 SQLite 数据库。
我的函数工作正常,但是当失败时,退出函数后文件仍然被锁定:
Public Function IsSqliteDB(ByVal uPath As String) As Boolean
'Workaround for my problem: Make a copy and work on this because this function is going to lock the file unfortunately
Dim sNewPath As String
sNewPath = Settings.Locations.Folder_LocalAppData_Temp & "\temp_" & Replace(Now.ToString, ":", "_") & ".db"
modIO.FileForceCopy(uPath, sNewPath)
Dim bIsSQLiteDB As Boolean = False
Dim c As New dhRichClient3.cConnection
Dim r As dhRichClient3.cRecordset
Try
Dim b As Boolean = c.OpenDB(sNewPath) 'returns true although this is not an sqlite-db. Can't do anything about it
R = c.OpenRecordset("SELECT * FROM sqlite_master")
bIsSQLiteDB = True
Catch ex As Exception
r = Nothing
c = Nothing
Finally
r = Nothing
c = Nothing
End Try
modIO.DeleteFile(sNewPath)'this fails. File is locked
Return bIsSQLiteDB
End Function
有人知道我可以在哪里确保文件不再被锁定吗?或者我真的必须使用该文件的副本吗,因为 COM 组件的行为并不真正已知(不幸的是闭源)?
我确实可以使用备份,但该文件可能非常大(> 1 GB),因此如果可以避免的话,我想避免制作副本来处理。
dhRichClient 中没有“关闭”功能。我猜,一旦 cConnection 超出范围,就会在内部调用“关闭”。在函数末尾调用 GC.Collect() 没有帮助。
要确定一个文件是否是 SQLite 数据库,只需检查该文件的前 16 个字节数据库头 http://www.sqlite.org/fileformat2.html#database_header.
来自 tmighty 的评论:
Public Function IsSqliteDB(ByVal uPath As String) As Boolean
Dim bytes(16) As Byte
Using fs As New IO.FileStream(uPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
fs.Read(bytes, 0, 16)
End Using
Dim text As String = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
Return text.Contains("SQLite format")
End Function
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)