Created
August 12, 2021 06:22
-
-
Save Benshi/d115f057b81aa61e257186dbea49f9db to your computer and use it in GitHub Desktop.
[VB] ファイルの占有サイズを取得する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Option Strict On | |
Imports System.ComponentModel | |
Imports System.IO | |
Imports System.Runtime.CompilerServices | |
Imports System.Runtime.InteropServices | |
Module FileInfoExtention | |
''' <summary> | |
''' ドライブのクラスターサイズを返します。 | |
''' </summary> | |
<Extension> | |
Public Function GetClusterSize(this As DriveInfo) As UInteger | |
Dim sectorsPerCluster, bytesPerSector, numberOfFreeClusters, totalNumberOfClusters As UInteger | |
If GetDiskFreeSpace(this.RootDirectory.FullName, sectorsPerCluster, bytesPerSector, numberOfFreeClusters, totalNumberOfClusters) Then | |
Return sectorsPerCluster * bytesPerSector | |
Else | |
Throw New Win32Exception() | |
End If | |
End Function | |
''' <summary> | |
''' 圧縮ファイルやオフラインファイルのファイルサイズを返します。 | |
''' </summary> | |
<Extension> | |
Public Function GetFileSize(this As FileInfo) As ULong | |
'Return this.Length | |
Const INVALID_FILE_SIZE As UInteger = UInteger.MaxValue | |
Dim low, high As UInteger | |
low = GetCompressedFileSize(this.FullName, high) | |
Dim win32Err = Marshal.GetLastWin32Error() | |
If high = 0 AndAlso low = INVALID_FILE_SIZE AndAlso win32Err <> 0I Then | |
Throw New Win32Exception(win32Err) | |
Else | |
Return (Convert.ToUInt64(high) << 32) + Convert.ToUInt64(low) | |
End If | |
End Function | |
''' <summary> | |
''' ディスク上のファイルサイズを返します。 | |
''' </summary> | |
''' <param name="clusterSize">クラスターサイズ</param> | |
<Extension> | |
Public Function GetFileSize(this As FileInfo, clusterSize As UInteger) As ULong | |
Dim size = this.GetFileSize() | |
Dim m = size Mod clusterSize | |
If m = 0 Then | |
Return size | |
Else | |
Return size + clusterSize - m | |
End If | |
End Function | |
Private Declare Auto Function GetCompressedFileSize Lib "kernel32" (lpFileName As String, <Out> ByRef lpFileSizeHigh As UInteger) As UInteger | |
Private Declare Auto Function GetDiskFreeSpace Lib "kernel32" (lpRootPathName As String, <Out> ByRef lpSectorsPerCluster As UInteger, <Out> ByRef lpBytesPerSector As UInteger, <Out> ByRef lpNumberOfFreeClusters As UInteger, <Out> ByRef lpTotalNumberOfClusters As UInteger) As Boolean | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment