· Una nueva versión de mi FileInfo personalizado, para obtener información sobre un archivo.
Public Class InfoFile
#Region " InfoFile "
' [ InfoFile ]
'
' // By Elektro H@cker
'
' Examples:
'
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.Name)) ' Result: Test
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.Extension_Without_Dot)) ' Result: txt
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.FileName)) ' Result: Test.txt
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.Directory)) ' Result: C:\
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.DriveRoot)) ' Result: C:\
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.DriveLetter)) ' Result: C
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.FullName)) ' Result: C:\Test.txt
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.ShortName)) ' Result: Test.txt
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.ShortPath)) ' Result: C:\Test.txt
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.Name_Length)) ' Result: 8
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.Extension_Without_Dot_Length)) ' Result: 3
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.FileName_Length)) ' Result: 8
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.Directory_Length)) ' Result: 3
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.FullName_Length)) ' Result: 11
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.FileSize_Byte)) ' Result: 5.127.975
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.FileSize_KB)) ' Result: 5.007.79
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.FileSize_MB)) ' Result: 4,89
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.FileSize_GB)) ' Result: 0,00
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.FileSize_TB)) ' Result: 0,00
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.FileVersion)) ' Result: ""
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.Attributes_Enum)) ' Result: 8224
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.Attributes_String)) ' Result: Archive, NotContentIndexed
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.CreationTime)) ' Result: 16/09/2012 8:28:17
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.LastAccessTime)) ' Result: 16/09/2012 10:51:17
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.LastModifyTime)) ' Result: 16/09/2012 10:51:17
' MsgBox(InfoFile.Get_Info("C:\Test.txt", InfoFile.Info.Has_Extension)) ' Result: True
Public Enum Info
Name ' Filename without extension
Extension_With_Dot ' File-Extension (with dot included)
Extension_Without_Dot ' File-Extension (without dot)
FileName ' Filename.extension
Directory ' Directory name
FullName ' Directory path + Filename
DriveRoot ' Drive letter
DriveLetter ' Drive letter (only 1 character)
ShortName ' DOS8.3 Filename
ShortPath ' DOS8.3 Path Name
Name_Length ' Length of Filename without extension
Extension_With_Dot_Length ' Length of File-Extension (with dot included)
Extension_Without_Dot_Length ' Length of File-Extension (without dot)
FileName_Length ' Length of Filename.extension
Directory_Length ' Length of Directory name
FullName_Length ' Length of Directory path + Filename
FileSize_Byte ' Size in Bytes
FileSize_KB ' Size in KiloBytes
FileSize_MB ' Size in MegaBytes
FileSize_GB ' Size in GigaBytes
FileSize_TB ' Size in TeraBytes
FileVersion ' Version for DLL or EXE files
Attributes_Enum ' Attributes as numbers
Attributes_String ' Attributes as descriptions
CreationTime ' Date Creation time
LastAccessTime ' Date Last Access time
LastModifyTime ' Date Last Modify time
Has_Extension ' Checks if file have a file-extension.
End Enum
Public Shared Function Get_Info
(ByVal File As String,
ByVal Information As Info
) As String
Dim File_Info
= My.
Computer.
FileSystem.
GetFileInfo(File)
Select Case Information
Case Info.Name : Return File_Info.Name.Substring(0, File_Info.Name.LastIndexOf("."))
Case Info.Extension_With_Dot : Return File_Info.Extension
Case Info.Extension_Without_Dot : Return File_Info.Extension.Split(".").Last
Case Info.FileName : Return File_Info.Name
Case Info.Directory : Return File_Info.DirectoryName
Case Info.DriveRoot : Return File_Info.Directory.Root.ToString
Case Info.DriveLetter : Return File_Info.Directory.Root.ToString.Substring(0, 1)
Case Info.FullName : Return File_Info.FullName
Case Info.
ShortName :
Return CreateObject("Scripting.FileSystemObject").
GetFile(File).
ShortName Case Info.
ShortPath :
Return CreateObject("Scripting.FileSystemObject").
GetFile(File).
ShortPath Case Info.Name_Length : Return File_Info.Name.Length
Case Info.Extension_With_Dot_Length : Return File_Info.Extension.Length
Case Info.Extension_Without_Dot_Length : Return File_Info.Extension.Split(".").Last.Length
Case Info.FileName_Length : Return File_Info.Name.Length
Case Info.Directory_Length : Return File_Info.DirectoryName.Length
Case Info.FullName_Length : Return File_Info.FullName.Length
Case Info.FileSize_Byte : Return Convert.ToDouble(File_Info.Length).ToString("n0")
Case Info.FileSize_KB : Return (Convert.ToDouble(File_Info.Length) / 1024L).ToString("n2")
Case Info.FileSize_MB : Return (Convert.ToDouble(File_Info.Length) / 1024L ^ 2).ToString("n2")
Case Info.FileSize_GB : Return (Convert.ToDouble(File_Info.Length) / 1024L ^ 3).ToString("n2")
Case Info.FileSize_TB : Return (Convert.ToDouble(File_Info.Length) / 1024L ^ 4).ToString("n2")
Case Info.
FileVersion :
Return CreateObject("Scripting.FileSystemObject").
GetFileVersion(File) Case Info.Attributes_Enum : Return File_Info.Attributes
Case Info.Attributes_String : Return File_Info.Attributes.ToString
Case Info.CreationTime : Return File_Info.CreationTime
Case Info.LastAccessTime : Return File_Info.LastAccessTime
Case Info.LastModifyTime : Return File_Info.LastWriteTime
Case Info.
Has_Extension :
Return IO.
Path.
HasExtension(File)
Case Else : Return String.Empty
End Select
End Function
#End Region
End Class
· Lo mismo de arriba pero para directorios:
Public Class InfoDir
#Region " InfoDir "
' [ InfoDir ]
'
' // By Elektro H@cker
'
' Examples:
'
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.Name)) ' Result: Test
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.Parent)) ' Result: Test Parent
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.FullName)) ' Result: C:\Test Parent\Test
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.DriveRoot)) ' Result: C:\
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.DriveLetter)) ' Result: C
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.Name_Length)) ' Result: 4
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.FullName_Length)) ' Result: 19
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.Attributes_Enum)) ' Result: 8208
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.Attributes_String)) ' Result: Directory, NotContentIndexed
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.CreationTime)) ' Result: 16/09/2012 8:28:17
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.LastAccessTime)) ' Result: 16/09/2012 10:51:17
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.LastModifyTime)) ' Result: 16/09/2012 10:51:17
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.FileSize_Byte)) ' Result: 5.127.975
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.FileSize_KB)) ' Result: 5.007.79
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.FileSize_MB)) ' Result: 4,89
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.FileSize_GB)) ' Result: 0,00
' MsgBox(InfoDir.Get_Info("C:\Test Parent\Test", InfoDir.Info.FileSize_TB)) ' Result: 0,00
Public Enum Info
Name ' Folder name
FullName ' Directory path
Parent ' Parent directory
DriveRoot ' Drive letter
DriveLetter ' Drive letter (only 1 character)
Name_Length ' Length of directory name
FullName_Length ' Length of full directory path
FileSize_Byte ' Size in Bytes (including subfolders)
FileSize_KB ' Size in KiloBytes (including subfolders)
FileSize_MB ' Size in MegaBytes (including subfolders)
FileSize_GB ' Size in GigaBytes (including subfolders)
FileSize_TB ' Size in TeraBytes (including subfolders)
Attributes_Enum ' Attributes as numbers
Attributes_String ' Attributes as descriptions
CreationTime ' Date Creation time
LastAccessTime ' Date Last Access time
LastModifyTime ' Date Last Modify time
End Enum
Public Shared Function Get_Info(ByVal Dir As String, ByVal Information As Info) As String
Dim Dir_Info = My.Computer.FileSystem.GetDirectoryInfo(Dir)
Select Case Information
Case Info.Name : Return Dir_Info.Name
Case Info.FullName : Return Dir_Info.FullName
Case Info.Parent : Return Dir_Info.Parent.ToString
Case Info.DriveRoot : Return Dir_Info.Root.ToString
Case Info.DriveLetter : Return Dir_Info.Root.ToString.Substring(0, 1)
Case Info.Name_Length : Return Dir_Info.Name.Length
Case Info.FullName_Length : Return Dir_Info.FullName.Length
Case Info.FileSize_Byte : Return Convert.ToDouble(Get_Directory_Size(Dir_Info)).ToString("n0")
Case Info.FileSize_KB : Return (Convert.ToDouble(Get_Directory_Size(Dir_Info)) / 1024L).ToString("n2")
Case Info.FileSize_MB : Return (Convert.ToDouble(Get_Directory_Size(Dir_Info)) / 1024L ^ 2).ToString("n2")
Case Info.FileSize_GB : Return (Convert.ToDouble(Get_Directory_Size(Dir_Info)) / 1024L ^ 3).ToString("n2")
Case Info.FileSize_TB : Return (Convert.ToDouble(Get_Directory_Size(Dir_Info)) / 1024L ^ 4).ToString("n2")
Case Info.Attributes_Enum : Return Dir_Info.Attributes
Case Info.Attributes_String : Return Dir_Info.Attributes.ToString
Case Info.CreationTime : Return Dir_Info.CreationTime
Case Info.LastAccessTime : Return Dir_Info.LastAccessTime
Case Info.LastModifyTime : Return Dir_Info.LastWriteTime
Case Else : Return String.Empty
End Select
End Function
Private Shared Function Get_Directory_Size(Directory As IO.DirectoryInfo) As Long
Try
Dim Dir_Total_Size
As Long = Directory.
EnumerateFiles().
Sum(Function(file) file.
Length) Dir_Total_Size += Directory.EnumerateDirectories().Sum(Function(dir) Get_Directory_Size(dir))
Return Dir_Total_Size
Catch
End Try
Return -1
End Function
#End Region
End Class
Convierte bytes a otra unidad:
#Region " Convert Bytes Function "
' [ Convert Bytes Function ]
'
' // By Elektro H@cker
'
' Examples :
'
' MsgBox(String.Format("{0} KB", Byte_To_Size(5127975, xByte.kilobyte, 2))) ' Result: 5007,79 KB
' MsgBox(String.Format("{0} MB", Byte_To_Size(5127975, xByte.megabyte, 2))) ' Result: 4,89 MB
' MsgBox(String.Format("{0} GB", Byte_To_Size(5127975, xByte.gigabyte, 3))) ' Result: 0,005 GB
' MsgBox(String.Format("{0} TB", Byte_To_Size(5127975, xByte.terabyte, 3))) ' Result: 0 TB
' MsgBox(String.Format("{0} PB", Byte_To_Size(5127975, xByte.petabyte, 3))) ' Result: 0 PB
Enum xByte As Long
kilobyte = 1024L
megabyte = 1024L * kilobyte
gigabyte = 1024L * megabyte
terabyte = 1024L * gigabyte
petabyte = 1024L * terabyte
End Enum
Private Function Byte_To_Size(ByVal bytes As Long, _
ByVal convertto As xByte, _
Optional ByVal decimals As Integer = 2 _
) As Double
Return (Convert.ToDouble(bytes) / convertto).ToString("n" & decimals)
End Function
#End Region