您的位置:首页 > 其它

对硬盘操作的API介绍以及应用测试

2016-07-13 14:28 405 查看
磁盘和驱动器管理API
GetLogicalDrivers       获取主机中所有的逻辑驱动器,以BitMap的形式返回.
GetLogicalDriverString    获取主机中所有的逻辑驱动器,以驱动器根路径字符串返回.
FindFirstVolume     查找主机中的第一个驱动器,返回查找句柄.
FindNextVolume      根据FindFirstVolume返回句柄,查找主机中后继的逻辑驱动器
FindVolumeClose     关闭驱动器查找句柄
GetDriveType      获取驱动器类型
GetVolumeInformation    获取逻辑驱动器信息
FindFirstVolumeMountPoint查找指定卷的第一个挂载点,返回查找句柄
FindNextVolumeMountPoint 根据FindFirstVolumeMountPoint返回的句柄,查找卷的后继挂载点.
FindVolumeMountPointClose 关闭挂载点查找句柄
GetVolumeNameForVolumeMountPoint 根据指定挂载点获取相应的卷设备名
SetVolumeMountPoint         将指定卷挂载到指定挂载点处
GetDiskFreeSpace            获取磁盘空间信息,包括每簇的扇区数,每扇区的字节数,簇数量,空闲的簇数量
GetDiskFreeSpaceEx          获取用户可用的空闲空间的字节数,磁盘总容量的字节数

原型:DWORD GetLogicalDrivers (void);

说明:函数返回值是一个long型,将其用二进制显示时,其中第0位表示A盘,第1位表示B盘,当某位为1时说明存在这个盘,即00000011表示有A盘和B盘。



2:GetLogicalDriverString    获取主机中所有的逻辑驱动器,以驱动器根路径字符串返回.

// operation_disk.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <WinDef.h>
#include <WinUser.h>
#include <string.h>
#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])
{
    int i=0;
    TCHAR buff[32] = {0};
    DWORD dw=GetLogicalDriveStrings(0,NULL);
    LPTSTR lpDriveStrings = buff;//=(LPTSTR)HeapAlloc(GetProcessHeap(),0,dw*sizeof(TCHAR));
    GetLogicalDriveStrings(dw,lpDriveStrings);

    scanf("%d",&i);
    return 0;
}



// operation_disk.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <Windows.h>

#include <WinDef.h>

#include <WinUser.h>

#include <string.h>

#include <stdlib.h>

int _tmain(int argc, _TCHAR* argv[])

{
int i=0;
TCHAR buff[32] = {0};
char c_buff[32] = {0};
DWORD dw=GetLogicalDriveStrings(0,NULL);
LPTSTR lpDriveStrings = buff;//=(LPTSTR)HeapAlloc(GetProcessHeap(),0,dw*sizeof(TCHAR));
GetLogicalDriveStrings(dw,lpDriveStrings);
memcpy(c_buff,buff,32);
char *p_buff = &c_buff[0];
for(int i = 0; i< 32; i++)
{
printf("%c",*p_buff);
p_buff = &c_buff[i];
//p_buff = NULL;
}

scanf("%d",&i);
return 0;

}

结果:



3:FindFirstVolume     查找主机中的第一个驱动器,返回查找句柄.



// operation_disk.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <WinDef.h>
#include <WinUser.h>
#include <string.h>
#include <stdlib.h>
#include <WinBase.h>
//#include <stdbool.h>

int _tmain(int argc, _TCHAR* argv[])
{
    int i=2;

    DWORD  Error                = ERROR_SUCCESS;
    HANDLE FindHandle           = INVALID_HANDLE_VALUE;
    BOOL   Success              = FALSE;
    WCHAR  VolumeName[MAX_PATH] = L"";//MAX_PATH   260
    //
    //  Enumerate all volumes in the system.
    FindHandle = FindFirstVolumeW(VolumeName, ARRAYSIZE(VolumeName));
    if (FindHandle == INVALID_HANDLE_VALUE)
    {
        Error = GetLastError();
        wprintf(L"FindFirstVolumeW failed with error code %d\n", Error);
        return -1;
    }
    printf("查找到的第一个卷的GUID路径为:\n");
    for (int i=0; i<256; i++)
    {
        printf("%c",VolumeName[i]);
    }

    while(1)
    {
        memset(VolumeName, 0 , ARRAYSIZE(VolumeName));
        Success = FindNextVolumeW(FindHandle, VolumeName, ARRAYSIZE(VolumeName));
        if ( !Success ) 
        {
            Error = GetLastError();
            if (Error != ERROR_NO_MORE_FILES) 
            {
                wprintf(L"FindNextVolumeW failed with error code %d\n", Error);
                break;
            }
            break;
        }
        printf("查找到的第[%d]个卷的GUID路径为:\n",i);
        i++;
        for (int i=0; i<256; i++)
        {
            printf("%c",VolumeName[i]);
        }
    }
    FindVolumeClose(FindHandle);//关闭驱动器查找句柄

        scanf("%d",&i);
        return 0;
}

4:GetDriveType获取驱动器类型







// operation_disk.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <WinDef.h>
#include <WinUser.h>
#include <string.h>
#include <stdlib.h>
#include <WinBase.h>
//#include <stdbool.h>

void rew(UINT bResult)
{
    switch(bResult)
    {
    case DRIVE_UNKNOWN: 
        printf("可获得驱动器类型为【无法识别的设备】\n", bResult);  
        break;
    case DRIVE_NO_ROOT_DIR: 
        printf("可获得驱动器类型为【给出的名字不存在,不存在的驱动器】\n", bResult);  
        break;
    case DRIVE_REMOVABLE: 
        printf("可获得驱动器类型为【可移动设备】\n", bResult);  
        break;
    case DRIVE_FIXED: 
        printf("可获得驱动器类型为【不可移动的磁盘】\n", bResult);  
        break;
    case DRIVE_REMOTE: 
        printf("可获得驱动器类型为【网络硬盘】\n", bResult);  
        break;
    case DRIVE_CDROM: 
        printf("可获得驱动器类型为【CD光驱】\n", bResult);  
        break;
    case DRIVE_RAMDISK: 
        printf("可获得驱动器类型为【内存虚拟盘】\n", bResult);  
        break;
    default:
        printf("program  Error\n");
        break;
    }
}

int _tmain(int argc, _TCHAR* argv[])
{
    int i=0;

    UINT bResult = 0;  
    LPCTSTR buff = _T("c:\\");

    //使用GetDiskFreeSpaceEx获取磁盘信息并打印结果   
    bResult = GetDriveType (buff);  
    printf("C盘:");
    rew(bResult);

    bResult = GetDriveType (_T("d:\\"));  
    printf("D盘:");
    rew(bResult);

    bResult = GetDriveType (_T("e:\\"));  
    printf("E盘:");
    rew(bResult);

    bResult = GetDriveType (_T("f:\\"));  
    printf("F盘:");
    rew(bResult);

    bResult = GetDriveType (_T("g:\\"));  
    printf("G盘:");
    rew(bResult);

    bResult = GetDriveType (_T("h:\\"));  
    printf("H盘:");
    rew(bResult);

        scanf("%d",&i);
        return 0;
}

5:GetDiskFreeSpaceEx获取用户可用的空闲空间的字节数,磁盘总容量的字节数



// operation_disk.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <WinDef.h>
#include <WinUser.h>
#include <string.h>
#include <stdlib.h>
#include <WinBase.h>
//#include <stdbool.h>

int _tmain(int argc, _TCHAR* argv[])
{
    int i=0;

    DWORD64 qwFreeBytesToCaller = 0, qwTotalBytes = 0, qwFreeBytes = 0;  
    DWORD dwSectPerClust = 0, dwBytesPerSect = 0, dwFreeClusters = 0,  dwTotalClusters = 0;  
    BOOL bResult = 0;  

    //使用GetDiskFreeSpaceEx获取磁盘信息并打印结果   
    bResult = GetDiskFreeSpaceEx (_T("c:\\"),  
        (PULARGE_INTEGER)&qwFreeBytesToCaller,  
        (PULARGE_INTEGER)&qwTotalBytes,  
        (PULARGE_INTEGER)&qwFreeBytes);  

    printf("可获得的空闲空间(GB): \t%I64d\n", qwFreeBytesToCaller/(1024*1024*1024));  
    printf("空闲空间(字节): \t\t%I64d\n", qwFreeBytes);  
    printf("磁盘总容量(字节): \t\t%I64d\n", qwTotalBytes); 

        scanf("%d",&i);
        return 0;
}

6:GetDiskFreeSpace获取磁盘空间信息,包括每簇的扇区数,每扇区的字节数,簇数量,空闲的簇数量



// operation_disk.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include <WinDef.h>
#include <WinUser.h>
#include <string.h>
#include <stdlib.h>
#include <WinBase.h>

int _tmain(int argc, _TCHAR* argv[])
{
    int i=0;

    DWORD dSectorsPerCluster=0;      //每簇的扇区数
    DWORD dBytesPerSector=0;            //每个扇区的字节数
    DWORD dNumberOfFreeClusters=0;   //可用簇数
    DWORD dTotalNumberOfClusters=0;  //总的簇数
    LPCWSTR pc = 0;
    if (GetDiskFreeSpace(_T("c:\\"),&dSectorsPerCluster,&dBytesPerSector,&dNumberOfFreeClusters,&dTotalNumberOfClusters)){
            printf("每蔟的扇区数为  %d\n",dSectorsPerCluster);
            printf("每个扇区的字节数为  %d\n",dBytesPerSector);
            printf("可用簇数为  %d\n",dNumberOfFreeClusters);
            printf("总的簇数为  %d\n",dTotalNumberOfClusters);
    }else{
        printf("error");
    }

        scanf("%d",&i);
        return 0;
}

7:GetVolumeInformation获取硬盘信息



// operation_disk_mfc.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "operation_disk_mfc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
    int nRetCode = 0;

    HMODULE hModule = ::GetModuleHandle(NULL);

    if (hModule != NULL)
    {
        // initialize MFC and print and error on failure
        if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
        {
            // TODO: change error code to suit your needs
            _tprintf(_T("Fatal Error: MFC initialization failed\n"));
            nRetCode = 1;
        }
        else
        {
            // TODO: code your application's behavior here.

            int i;
            /*LPWSTR lpVolumeNameBuffer = 0;
            LPDWORD  SerialNumber = 0;
            DWORD nVolumeNameSize = 0;
            GetVolumeInformation( _T("c:"),lpVolumeNameBuffer,255,SerialNumber,&MaxCLength,&FileSysFlag,FileSysName,255);*/
            DWORD   VolumeSerialNumber = 0; 
            TCHAR   VolumeName[256] = _T(""); 
            if (GetVolumeInformation( _T("d:\\"),VolumeName,12,&VolumeSerialNumber,NULL,NULL,NULL,10) < 0)
            {
                printf("D盘error\n");
            }
            printf( "D盘的序列号:%lu\n ",VolumeSerialNumber); 

            VolumeSerialNumber = 0;
            if (GetVolumeInformation( _T("c:\\"),VolumeName,12,&VolumeSerialNumber,NULL,NULL,NULL,10) < 0)
            {
                printf("c盘error\n");
            }
            printf( "C盘的序列号:%lu\n ",VolumeSerialNumber); 

            scanf("%d",&i);
        }
    }
    else
    {
        // TODO: change error code to suit your needs
        _tprintf(_T("Fatal Error: GetModuleHandle failed\n"));
        nRetCode = 1;
    }

    return nRetCode;
}

8:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: