小弟最近玩硬盘的东东。
现在有一个问题,按道理说虽然不难,但是怎样都想不出来。就是:
假设我有多个硬盘,怎样列出某个物理硬盘上所有分区的盘符呢??用Win98/2000 API可以吗?还是只能用WMI,具体怎样做呢?

解决方案 »

  1.   

    如果是在Windows2000或WindowsXP中还可以用 WMI 来帮忙。
    (注:下面用BASIC语言描述)例如:
    1、取的物理硬盘的信息:   Dim DiskDriveSet 
       Dim dd
       Set DiskDriveSet = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
                                               InstancesOf("Win32_DiskDrive")
           
       For Each dd In DiskDriveSet
               'identification info
       Debug.Pring  dd.Index '//硬盘的编号,如 0、1、2……
       Debug.Pring  dd.DeviceID '//设备ID,如\\.\PHYSICALDRIVE1
       Debug.Pring  dd.Model        '//设备型号,如IC35L040AVER07-0(这个是IBM Deskstar 40G 硬盘的型号,不知道这个
    是不是就是论坛经常提到的 硬盘序列号。)
       Debug.Pring  dd.MediaType    '//设备介质类型    'physical info
       Debug.Pring  FormatNumber(dd.Size, 0) '//盘的容量
       Debug.Pring  dd.Partitions '//有多少个分区
       Debug.Pring  FormatNumber(dd.BytesPerSector, 0)   
       Debug.Pring  FormatNumber(dd.SectorsPerTrack, 0)
       Debug.Pring  FormatNumber(dd.TotalCylinders, 0)   Next '//如果有多个硬盘则显示下一个硬盘的信息。2、取得逻辑盘的信息
       dim LogicalDiskSet 
       dim ld
       Set LogicalDiskSet = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
                                      InstancesOf("Win32_LogicalDisk")
        
        
       For Each ld In LogicalDiskSet
    '元素有很多下面仅列出三个,其他的可以查看MSDN
    Debug.Pring ld.Name 
    Debug.Pring ld.Size 
    Debug.Pring ld.VolumeName
    Debug.Pring ld.VolumeSerialNumber 
       Next
    不过…………………… 
    他们的功能都是独立,没有关联的。
    即我不可以得到一个物理硬盘的所有分区的盘符是什么!!!按道理用 WMI 的 ASSOCIATORS 可以。
    例如用这样的方法可以得出某个盘符是属于那个物理盘(假设我有多个硬盘)Set DiskSet = GetObject("winmgmts:").ExecQuery("Associators of {win32_LogicalDisk='C:'} where ResultClass = Win32_DiskPartition")For Each dd In DiskSet 
        Debug.Print dd.Name & " => " & dd.DiskIndex
    Next可是反过来做却不行:Set DiskSet = GetObject("winmgmts:").ExecQuery("Associators of {Win32_DiskDrive=0} where ResultClass = win32_LogicalDisk")
    For Each dd In DiskSet 
        Debug.Print dd.Name
    Next所以…………
    问题仍然未解决。相关的资料可以看:
    http://www.csdn.net/Expert/TopicView1.asp?id=895291
    http://www.csdn.net/Expert/TopicView1.asp?id=893191希望热衷于研究编程的同志们一同动手把它解决掉 :))
      

  2.   

    DWORD dwDrive = GetLogicDrive();
    int nPos(0);
    CString strDrive = "?:";while(dwDrive)
    {
         if (dwDrive & 1)
              strDrive.SetAt(0,'A'+nPos);
         dwDrive >>= 1;        
    }
      

  3.   

    先用这个:
    The GetLogicalDriveStrings function fills a buffer with strings that specify valid drives in the system. DWORD GetLogicalDriveStrings(
      DWORD nBufferLength,  // size of buffer
      LPTSTR lpBuffer       // pointer to buffer for drive strings
    );
     
      

  4.   

    再用这个:
    The GetDriveType function determines whether a disk drive is a removable, fixed, CD-ROM, RAM disk, or network drive. UINT GetDriveType(
      LPCTSTR lpRootPathName   // pointer to root path
    );
     
      

  5.   

    这里有一个使用上述API的例子:
    http://tech.enet.com.cn/document/20020417/2002041711011801.shtml
      

  6.   

    给你一个例子:Example/* GETDRIVE.C illustrates drive functions including:
     *      _getdrive       _chdrive        _getdcwd
     */#include <stdio.h>
    #include <conio.h>
    #include <direct.h>
    #include <stdlib.h>
    #include <ctype.h>void main( void )
    {
       int ch, drive, curdrive;
       static char path[_MAX_PATH];   /* Save current drive. */
       curdrive = _getdrive();   printf( "Available drives are: \n" );   /* If we can switch to the drive, it exists. */
       for( drive = 1; drive <= 26; drive++ )
          if( !_chdrive( drive ) )
             printf( "%c: ", drive + 'A' - 1 );   while( 1 )
       {
          printf( "\nType drive letter to check or ESC to quit: " );
          ch = _getch();
          if( ch == 27 )
             break;
          if( isalpha( ch ) )
             _putch( ch );
          if( _getdcwd( toupper( ch ) - 'A' + 1, path, _MAX_PATH ) != NULL )
             printf( "\nCurrent directory on that drive is %s\n", path );
       }   /* Restore original drive.*/
       _chdrive( curdrive );
       printf( "\n" );
    }
    OutputAvailable drives are:
    A: B: C: L: M: O: U: V: 
    Type drive letter to check or ESC to quit: c
    Current directory on that drive is C:\CODEType drive letter to check or ESC to quit: m
    Current directory on that drive is M:\Type drive letter to check or ESC to quit: 
     
      

  7.   

    感谢各位了!!
    不过我要的是列出“某个”物理硬盘上所有分区的盘符。
    例如我有两个硬盘: 1、2
    第一个硬盘分了2个区,第二个硬盘分了3个区,则在win98中
    C: E: 是属于第一个硬盘的。
    D: F: G: 是第二个硬盘的。问题的关键是要用程序来检测上述的结果。
    还是没解决呀!emm...