在VC运行环境下运用什么API函数可以得到操作系统所在的分区。比如操作系统WIN2000在C盘或D盘目录为WINNT,运用什么API函数可以返回出C:\WINNT或D:\WINNT

解决方案 »

  1.   

    TCHAR szSysDir[MAX_PATH+25];
    GetSystemDirectory(szSysDir,MAX_PATH);
    用此函数可得到如"c:\winnt\system32"之后可以分析字符串。
      

  2.   

    vb 中的声明
    Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As LonglpBuffer String,用于装载系统目录路径名的一个字串缓冲区。它应事先初始化成nSize+1个字符的长度。通常至少要为这个缓冲区分配MAX_PATH个字符的长度 
    nSize Long,lpBuffer字串的最大长度 
      

  3.   

    得到Windows目录
    Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    得到Windows系统目录
    Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    得到临时文件存放目录
    Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
    得到当前目录
    Private Declare Function GetCurrentDirectory Lib "kernel32" Alias "GetCurrentDirectory" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
      

  4.   

    ////www.mvps.org/vbnet 是学VB的好去处另外还有其它方法Public Enum SPECIAL_FOLDERS
        'Windows desktop virtual folder at the root of the name space
        vbCSIDL_DESKTOP = &H0&   'File system directory that contains the
       'user's program groups (which are also file   'system directories)
        vbCSIDL_PROGRAMS = &H2&
       'Control Panel - virtual folder containing
       'icons for the control panel applications
        vbCSIDL_CONTROLS = &H3&
       'Printers folder - virtual folder containing    'installed printers.
        vbCSIDL_PRINTERS = &H4&   'File system directory that serves as a
       'common repository for documents (My Documents folder)
        vbCSIDL_PERSONAL = &H5&
       'File system directory that contains the
       'user's favorite Internet Explorer URLs
        vbCSIDL_FAVORITES = &H6&
       'File system directory that corresponds to the
       'user's Startup program group
        vbCSIDL_STARTUP = &H7&
       'File system directory that contains the
       'user's most recently used documents (Recent folder)
        vbCSIDL_RECENT = &H8&   'File system directory that contains
       'Send To menu items    Public Const
        vbCSIDL_SENDTO = &H9&
       'Recycle bin file system directory containing file
       'objects in the user's recycle bin. The location of
       'this directory is not in the registry; it is ed
       'with the hidden and system attributes to prevent the
       'user from moving or deleting it.
        vbCSIDL_BITBUCKET = &HA&
       'File system directory containing Start menu items
        vbCSIDL_STARTMENU = &HB&
       'File system directory used to physically store
       'file objects on the desktop (not to be confused
       'with the desktop folder itself).
        vbCSIDL_DESKTOPDIRECTORY = &H10&
       'My Computer - virtual folder containing everything
       'on the local computer: storage devices, printers,
       'and Control Panel. The folder may also contain    'mapped network drives.
        vbCSIDL_DRIVES = &H11&
       'Network Neighborhood - virtual folder representing
       'the top level of the network hierarchy
        vbCSIDL_NETWORK = &H12&
       'File system directory containing objects that
       'appear in the network neighborhood
        vbCSIDL_NETHOOD = &H13&
       'Virtual folder containing fonts
        vbCSIDL_FONTS = &H14&
       'File system directory that serves as a
       'common repository for document templates    '(ShellNew folder.)
        vbCSIDL_TEMPLATES = &H15&
    End Enum
    Private Declare Function SHGetSpecialFolderLocation Lib "shell32.dll" _
              (ByVal hWndOwner As Long, _
              ByVal nFolder As SPECIAL_FOLDERS, _
              pidl As Long) As Long'Converts an item identifier list to a file system path.
    Private Declare Function SHGetPathFromIDList Lib "shell32.dll" _
               Alias "SHGetPathFromIDListA" _
              (ByVal pidl As Long, _
               ByVal pszPath As String) As Long'This fuction is courtesy of Randy Birch and VBNet <www.mvps.org/vbnet>
    'however I changed it a bit to fit my class
    Private Function GetSpecialFolderLocation(CSIDL As SPECIAL_FOLDERS) As String   Dim sPath As String
       Dim pidl As Long
       
      'fill the idl structure with the specified folder item
       If SHGetSpecialFolderLocation(m_hWnd, CSIDL, pidl) = NOERROR Then
         
         'if the pidl is returned, initialize
         'and get the path from the id list
          sPath = Space$(MAX_PATH)
          
          If SHGetPathFromIDList(ByVal pidl, ByVal sPath) Then        'free the pidl and return the path
             Call CoTaskMemFree(ByVal VarPtr(pidl))
             GetSpecialFolderLocation = left(sPath, InStr(sPath, Chr$(0)) - 1)
             
          End If
        
        End If
       
    End Function
      

  5.   

    添加补充
    Private Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal pv As Long)
    Private Const NOERROR As Long = &H0////
    上面是一个类模块中的函数, 稍加修改就可方便调用了, 可以获得一些常用的目录