آموزش توابع API ویندوز گروه: Drives منبع: انجمن تخصصی برنامه نویسان ایران - http://www.iranvig.com
GetDiskFreeSpace, GetDiskFreeSpaceEx, GetDriveType, GetLogicalDrives, GetLogicalDriveStrings,
' Calculate and display the free and total space on drive C:
Dim secPerClus As Long ' receives sectors per cluster
Dim bytePerSec As Long ' receives bytes per sector
Dim freeClus As Long ' receives number of free clusters
Dim totalClus As Long ' receives total number of clusters
Dim retval As Long ' return value
' Read the information into the variables
retval = GetDiskFreeSpace("c:\", secPerClus, bytePerSec, freeClus, totalClus)
' Display the information
Debug.Print "Free space:"; freeClus * secPerClus * bytePerSec; "bytes"
Debug.Print "Total space:"; totalClus * secPerClus * bytePerSec; "bytes" Display the total free space available on drive C:. Because Visual Basic doesn't have good support for the 64-bit integers needed for modern hard drive's free spaces, the workaround described on this page is needed to display the values properly. This example runs when the user click button Command1, so to use this example, you need to place a command button named Command1 on a form window.
' Declarations and such needed for the example:
' (Copy them to the (declarations) section of a module.)
Public Type ULARGE_INTEGER
LowPart As Long
HighPart As Long
End Type
Public Declare Function GetDiskFreeSpaceEx Lib "kernel32.dll" Alias "GetDiskFreeSpaceExA" (ByVal _
lpDirectoryName As String, lpFreeBytesAvailableToCaller As ULARGE_INTEGER, _
lpTotalNumberOfBytes As ULARGE_INTEGER, lpTotalNumberOfFreeBytes As ULARGE_INTEGER) As Long
Public Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (Destination As Any, Source _
As Any, ByVal Length As Long)
' *** Place the following code inside the form window. ***
Private Sub Command1_Click()
Dim userbytes As ULARGE_INTEGER ' bytes free to user
Dim totalbytes As ULARGE_INTEGER ' total bytes on disk
Dim freebytes As ULARGE_INTEGER ' free bytes on disk
Dim tempval As Currency ' display buffer for 64-bit values
Dim retval As Long ' generic return value
' Get information about the C: drive.
retval = GetDiskFreeSpaceEx("C:\", userbytes, totalbytes, freebytes)
' Copy freebytes into the Currency data type.
CopyMemory tempval, freebytes, 8
' Multiply by 10,000 to move Visual Basic's decimal point to the end of the actual number.
tempval = tempval * 10000
' Display the amount of free space on C:.
Debug.Print "Free Space on the C: drive:"; tempval; "bytes"
End Sub ' Determine what type of drive D: is
Dim drivetype As Long ' receives the drive type
drivetype = GetDriveType("d:\") ' determine which kind of drive this is
If drivetype = 1 Then Debug.Print "Drive D:\ does not exist."
If drivetype = DRIVE_REMOVABLE Then Print "Drive D:\ is a removable-disk drive."
If drivetype = DRIVE_FIXED Then Print "Drive D:\ is a hard drive."
If drivetype = DRIVE_CDROM Then Print "Drive D:\ is a CD-ROM drive."
' etc. ' Tell the user which drives exist on the computer. Note how this example
' only checks up to drive D:, but it does establish the necessary pattern to use in general.
Dim driveflags As Long ' receives the flags identifying valid drives
' Get the valid logical drives on the computer.
driveflags = GetLogicalDrives()
' Test the returned value to see if drives A: through D: exist.
If (driveflags And 1) = 1 Then Debug.Print "Drive A: exists."
If (driveflags And 2) = 2 Then Debug.Print "Drive B: exists."
If (driveflags And 4) = 4 Then Debug.Print "Drive C: exists."
If (driveflags And 8) = 8 Then Debug.Print "Drive D: exists."
' And so on.... ' List the names of all the root directories. Since each entry in the string takes
' four characters (three for the name and one for the null), we can "count by fours" going
' through the string until we reach the end. This frees us from worrying about nulls.
Dim drivenames As String ' receives list of root names
Dim thisdrive As String ' buffer for one extracted root directory name
Dim c As Long ' counter variable
Dim slength As Long ' receives length of returned string
' Make enough room in the buffer to receive the drive names.
drivenames = Space(255) ' more than enough room
' Get the root directory names of all logical drives.
slength = GetLogicalDriveStrings(255, drivenames) ' drivenames now holds the list
' Count by fours to extract the names of each drive.
For c = 1 To slength Step 4 ' loop with an increment of 4
thisdrive = Mid(drivenames, c, 3) ' extract a 3-character string X:\ (X is the drive letter)
Debug.Print thisdrive ' display the drive name
Next c
نام تابع: GetDiskFreeSpace
اعلان: Declare Function GetDiskFreeSpace Lib "kernel32.dll" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) As Long GetFileAttributes Lib "kernel32.dll" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
سيستم عامل: 2000 , NT , 98 , 95
توضيحات: اين تابع يك سري اطلاعات راجع به فضاي خالي يك ديسك به ما ميدهد . اين اطلاعات شامل تعداد سكتورهاي (Sector) هر كلاستر (Cluster) و تعداد بايت هاي يك سكتور و تعداد كلاستر هاي خالي و تعداد كل كلاستر ها .بدليل وجود محدوديت در عدد (حداكثر 32بيتي) اين دستور در نهايت ميتواند براي هار ديسكهاي حداكثر 2MB(دو مگا بايت) پاسخگو باشد.ولي تابع GetDiskFreeSpaceEx اين محدوديت را ندارد.
مقدار بازگشتي: اين تابع صفر بر ميگرداند اگر به خطا بر بخورد (GetLastErrorنوع خطا را مشخص ميكند) وغير صفر برميگرداند اگر موفق باشد.
پارامترها: lpRootPathName محل روت اصلي ديسكي كه اطلاعات آن مورد نياز است . مانند A:\ و يا C:\ lpSectorsPerCluster يك عدد 32بيتي كه تعداد سكتور هاي يك كلاستر را نشان ميدهد. lpBytesPerSector يك عدد 32بيتي كه تعداد بايتهاي هر سكتور را نمايش ميدهد. lpNumberOfFreeClusters يك عدد 32بيتي كه تعداد كلاسترهاي خالي را نشان ميدهد .در ويندوز 2000اين عدد امكان دارد با واقعيت همخواني نداشته باشد بستگي به سهم تعيين شده براي كاربران دارد. lpTotalNumberOfClusters يك عدد 32 بيتي كه تعداد كل كلاسترهاي استفاده شده و نشده را نشان ميدهد. در ويندوز 2000اين عدد امكان دارد با واقعيت همخواني نداشته باشد بستگي به سهم تعيين شده براي كاربران دارد.
ثابتهاي مورد استفاده: ---
کتابخانه: Kernel32
توابع مرتبط: GetDiskFreeSpaceEx
نکات: ---
کد نمونه:
نام تابع: GetDiskFreeSpaceEx
اعلان: Declare Function GetDiskFreeSpaceEx Lib "kernel32.dll" Alias "GetDiskFreeSpaceExA" (ByVal lpDirectoryName As String, lpFreeBytesAvailableToCaller As ULARGE_INTEGER, lpTotalNumberOfBytes As ULARGE_INTEGER, lpTotalNumberOfFreeBytes As ULARGE_INTEGER) As Long
سيستم عامل: 2000 , NT , 98
توضيحات: اين تابع سايز ديسك را نشان ميدهد.اين تابع فضاي خالي موجود كاربر و فضاي كل ديسك و مقدار فضاي خالي ديسك را ميدهد.هر يك از مقادير فوق درون يك ساختار با نام ULARGE_INTEGER قرارميگيرد كه يك عدد 64 بيتي صحيح ميباشد البته جهت استفاده از آن بايد زبان برنامه نويسي برنامه تان قابليت استفاده از عدد 64 بيتي را دارا باشد.
مقدار بازگشتي: اگر به خطا برسد مقدار صفر برميگرداند (GetLastError نوع خطا را مشخص ميكند) و در صورت موفقيت مقدار غير صفر برميگرداند.
پارامترها: lpDirectoryName نام دايركتوري موجود در ديسك كه اطلاعات آن مورد نياز است. lpFreeBytesAvailableToCaller عدد صحيح بدون علامت 64بيتي كه مقدار فضاي خالي ديك را بر حسب بايت ميدهد . در ويندوز 2000 اين مقدار امكان دارد با واقعيت همخوان نباشد و بستكي به مقدار تخصيص يافته به كاربر دارد. lpTotalNumberOfBytes يك عدد بدون علامت 64بيتي كه كل فضاي ديسك را نشان ميدهد (برحسب بايت) در وينوز 2000 اين مقدار شايد با واقعيت هم خواني نداشته باشد و بستگي به مقدار تخصيص يافته به كاربر دارد. lpTotalNumberOfFreeBytes يك عدد 64 بيتي بدون علامت كه كل فضاي خالي ديسك را نشان ميدهد.
ثابتهاي مورد استفاده: ---
کتابخانه: Kernel32
توابع مرتبط: GetDiskFreeSpace
نکات: ---
کد نمونه:
نام تابع: GetDriveType
اعلان: Declare Function GetDriveType Lib "kernel32.dll" Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
سيستم عامل: 32s , NT , 98 , 95
توضيحات: اين تابع نوع ديسك يك ديسك ديرايو را تعيين ميكند. اين تابع ميتواند براي هار ديسك و فلاپي ديسك و سي دي رم (CDROM) مورد استفاده قرار گيرد. برگشت عدد صفر به معني خطا و عدد يك يعني درايوي پيدا نشد و عدد هاي ديگر نوع درايو را مشخص ميكند كه يكي از مقادير زير است. DRIVE_CDROM = 5 درايو از نوع CDROM DRIVE_FIXED = 3 هارد ديسك DRIVE_RAMDISK = 6 ديسك حافظه اي (RAMDISK) DRIVE_REMOTE = 4 ديسك شبكه اي(تعريف شده در شبكه) DRIVE_REMOVABLE = 2 يك فلاپي درايو nDrive نام درايو مانند C:\ ويا A:\
مقدار بازگشتي: ---
پارامترها: ---
ثابتهاي مورد استفاده: ---
کتابخانه: Kernel32
توابع مرتبط: GetLogicalDriveStrings
نکات: ---
کد نمونه:
نام تابع: GetLogicalDrives
اعلان: Declare Function GetLogicalDrives Lib "kernel32.dll" () As Long
سيستم عامل: 32s , NT , 98 , 95
توضيحات: اين تابع همه درايوهاي منطقي موجود در كامپيوتر را تعيين ميكند. درايو منطقي با يك حرف مشخص ميشود مانند A: يا C: اين تابع براي هر درايو يك بيت برميگرداند با استفاده از عملگرAND شما ميتوانيد ديرايوها را مشخص نماييد .مثلا براي مشخص شدن وجود درايو C كافيست مقدار برگشتي را با عدد 4 تركيبAND نماييد. و ... ( به مثال توجه كنيد تا بيشتر متوجه شويد)
مقدار بازگشتي: ---
پارامترها: ---
ثابتهاي مورد استفاده: ---
کتابخانه: Kernel32
توابع مرتبط: GetLogicalDriveStrings
نکات: ---
کد نمونه:
نام تابع: GetLogicalDriveStrings
اعلان: Declare Function GetLogicalDriveStrings Lib "kernel32.dll" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
سيستم عامل: 32s , NT , 98 , 95
توضيحات: اين تابع لاجيكال درايوهاي موجود در كامپيوتر را تعيين ميكند و نام آنها به همراه روت (Root) آنها را داخل يك استرينگ پاس داده شده به نام lpBuffer . هر روت دايركتوري در اين بافر يا ديگري با كاراكتر Null جدا ميشود . مثلا اگر دو درايو داشته باشيم مانند A:وC: استرينگ برگشتي به صورت زير است "A:\(null)C:\(null)(null)” . اين تابع صفر برميگراند اگر به خطا بر بخورد و درغير آن طول استرينگ lpBuffer را برميگرداند. nBufferLength طول بافر عبور داده شده lpBuffer. lpBuffer نام همه لاجيكال درايوها . اين بافر بايد به اندازه كافي حجم داشته باشد.
مقدار بازگشتي: ---
پارامترها: ---
ثابتهاي مورد استفاده: ---
کتابخانه: Kernel32
توابع مرتبط: GetLogicalDrives
نکات: ---
کد نمونه:
