MFC读写磁盘信息

来源:互联网 发布:小米2s4g网络怎么设置 编辑:程序博客网 时间:2024/05/17 01:11

有五个基本的函数:GetLogicalDrives, GetLogicalDriveStrings, GetDriveType , GetVolumeInformation, SetVolumeLabel . 下面分别说下:

1  GetLogicalDrives

DWORD WINAPI GetLogicalDrives(void);

Retrieves a bitmask (位掩码)representing the currently available disk drives. If the function fails, the return value is zero

0位表示A盘,1位表示B盘, 2位表示C盘,以此类推. 我电脑上运行, 返回值是124 ,124转二进制1111100,所以有C ,D, E,F,G五个盘符

2 GetLogicalDriveStringsW

DWORD WINAPI GetLogicalDriveStrings( _In_   DWORD nBufferLength, _Out_  LPTSTR lpBuffer);

以char array 的方式返回盘符信息, 格式是"C:\\<NULL>D:\<NULL>E:\<NULL><NULL>" 

注意 :必须用LPTSTR数组接收返回信息,不能用CString . 如果用CString只能接收第一个<NULL>之前的信息.

CStringList ValumeList;TCHAR buf[100];CString strDriverList;LPCTSTR lpRootPathName=TEXT("c:\\"); //取C盘LPTSTR lpVolumeNameBuffer= new WCHAR[12];//磁盘卷标DWORD nVolumeNameSize=12;// 卷标的字符串长度DWORD VolumeSerialNumber;//硬盘序列号DWORD MaximumComponentLength;// 最大的文件长度LPTSTR lpFileSystemNameBuffer=new WCHAR[10];// 存储所在盘符的分区类型的长指针变量DWORD nFileSystemNameSize=10;// 分区类型的长指针变量所指向的字符串长度DWORD FileSystemFlags;// 文件系统的一此标志GetLogicalDriveStringsW(100, buf);//"C:\\<NULL>D:\<NULL>E:\<NULL><NULL>"int count =0 ;while(count<100){if(buf[count] ==':' ){CString strtemp ;strtemp.AppendChar(buf[count-1]);strtemp.AppendChar(buf[count]);strtemp.AppendChar(buf[count+1]);//strtemp.AppendChar(buf[count+2]);//'/0'ValumeList.AddTail(strtemp);}count++;}

 

3 GetDriveType 

UINT WINAPI GetDriveType( _In_opt_  LPCTSTR lpRootPathName);

Determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive.

LPCTSTR RootPathName=TEXT("F:\ ");int type = GetDriveType(   RootPathName);

返回值定义在winbase.h中:

#define DRIVE_UNKNOWN     0
#define DRIVE_NO_ROOT_DIR 1
#define DRIVE_REMOVABLE   2
#define DRIVE_FIXED       3
#define DRIVE_REMOTE      4
#define DRIVE_CDROM       5
#define DRIVE_RAMDISK     6

4 GetVolumeInformation

Retrieves information about the file system and volume associated with the specified root directory.

BOOL WINAPI GetVolumeInformation( _In_opt_   LPCTSTR lpRootPathName,

_Out_opt_  LPTSTR lpVolumeNameBuffer,

_In_       DWORD nVolumeNameSize,

 _Out_opt_  LPDWORD lpVolumeSerialNumber,

 _Out_opt_  LPDWORD lpMaximumComponentLength,

 _Out_opt_  LPDWORD lpFileSystemFlags,

 _Out_opt_  LPTSTR lpFileSystemNameBuffer,

 _In_       DWORD nFileSystemNameSize);

for(int i=0;i<ValumeList.GetCount();i++){       lpRootPathName= ValumeList.GetAt(ValumeList.FindIndex(i));  //用stringlist内容设置盘符        int returnv = GetVolumeInformationW(lpRootPathName,     //获取盘符信息        lpVolumeNameBuffer, nVolumeNameSize,       &VolumeSerialNumber, &MaximumComponentLength,       &FileSystemFlags,       lpFileSystemNameBuffer, nFileSystemNameSize);       CString strVolumeName = lpVolumeNameBuffer ;       if(strVolumeName.Compare(Parameter1)==0)       {          wcout<< Parameter1.GetBuffer()<<TEXT("--->")<<ValumeList.GetAt(ValumeList.FindIndex(i)).GetBuffer()<<endl;          break;       }      }    


5 SetVolumeLabel

Sets the label of a file system volume. 设置盘符的卷标,如果错误返回0 .

if( SetVolumeLabelW(argv[2],argv[3])) //related to UAC  ,$err,hr = getlasterror(){ wcout<<TEXT("SetVolumeLabel pass")<<endl;}else{ wcout<<TEXT("SetVolumeLabel fail")<<endl;}

注意:win7上需要调低用户控制权限, 否则设置会失败. //修改用户权限后重启有效哦

 

 

   


 

 

 

 

 

原创粉丝点击