你可以用GetLogicalDriveString更方便
解决方案 »
- 关于Skin++导致列表控件不能显示实时数据的问题
- build提示框
- dc.GetSafeHDC()
- 动态二维数组的问题..急
- 如果让一个biBitCount=8的位图与一个DC兼容!
- 应该用怎么样的代码实现以下问题??(请高手急救)
- 当了一个程序snmp agent不知道干什么的?
- 今天用了VC++.NET 2003 7.1发现以前在VC++6中的Ctrl+W的快捷键怎么没有了???ClassWinzard
- 如何获得QQ程序主窗口的句柄和标题(就是QQ号)呢
- ScrollView中如何加按钮?
- 在console程序中,有大量数据正在不断输出时,如何实现ctrl-c功能,使程序停止输出
- 编译错误!高手请进!
--------------------------------------------------------------------------------
The information in this article applies to:Microsoft Visual Basic Professional Edition for Windows, versions 5.0, 6.0
Microsoft Visual Basic Enterprise Edition for Windows, versions 5.0, 6.0--------------------------------------------------------------------------------
SUMMARY
You can find the drive letter of any CD-ROM drive on a local computer by using the Win32 API from Visual Basic. This can be useful during scenarios such as program setup and querying a user for media. MORE INFORMATION
You can use the GetDriveType API to determine the type of drive referenced by a drive letter. By combining this API with a list of the active drive letters on a computer, you can search for CD-ROM drives available on that computer. Note that the API does not distinguish between types of CD-ROM drives, so for example, a DVD drive is considered a CD-ROM drive.The following code sample demonstrates how to search for local CD-ROM drives by using Visual Basic. Sample Code
Start a new Visual Basic Standard EXE project. Form1 is created by default.
Add a new command button (Command1) to Form1.
Add the following code to Form1's code window:Option ExplicitPrivate Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" _
(ByVal nDrive As String) As LongPrivate Declare Function GetLogicalDriveStrings Lib "kernel32" _
Alias "GetLogicalDriveStringsA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As LongConst DRIVE_CDROM& = 5Public Function GetDriveStrings() As String
' Wrapper for calling the GetLogicalDriveStrings api
Dim result As Long ' Result of our API calls
Dim strDrives As String ' String to pass to API call
Dim lenStrDrives As Long ' Length of the above string
' Call GetLogicalDriveStrings with a buffer size of zero to
' find out how large our stringbuffer needs to be
result = GetLogicalDriveStrings(0, strDrives)
strDrives = String(result, 0)
lenStrDrives = result
' Call again with our new buffer
result = GetLogicalDriveStrings(lenStrDrives, strDrives)
If result = 0 Then
' There was some error calling the API
' Pass back an empty string
' NOTE - TODO: Implement proper error handling here
GetDriveStrings = ""
Else
GetDriveStrings = strDrives
End If
End FunctionPrivate Sub Command1_Click()
Dim strDrives As String
' Find out what drives we have on this machine
strDrives = GetDriveStrings()
If strDrives = "" Then
' No drives were found
MsgBox "No Drives were found!", vbCritical
Else
' Walk through the string and check the type of each drive
' displaying any cd-rom drives we find
Dim pos As Long
Dim drive As String
Dim drivetype As Long
pos = 1
Do While Not Mid$(strDrives, pos, 1) = Chr(0)
drive = Mid$(strDrives, pos, 3)
pos = pos + 4
drivetype = GetDriveType(drive)
If drivetype = DRIVE_CDROM Then
MsgBox "CD-ROM found at drive " & UCase(drive)
End If
Loop
End If
End Sub
Click Run or press the F5 key to run the project.
Result: Any CD-ROM drives on the computer are displayed by drive letter in a message box. REFERENCES
For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base: Q190000 HOWTO: Get Started Programming with the Windows API
Q180766 VBA: Sample Code to Determine CD-ROM Drive Letter
The Visual Basic Programmer's Guide© Microsoft Corporation 2001, All Rights Reserved.
Contributions by Gray McDonald, Microsoft Corporation
Additional query words: VB CD CDROM Locate Drive Find List Keywords : kbsample kbAPI kbSDKWin32 kbVBp kbVBp500 kbVBp600 kbGrpDSVB kbDSupport
Issue type : kbhowto
Technology : kbWord600Search
Last Reviewed: March 26, 2001
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.
--------------------------------------------------------------------------------
Send feedback to MSDN.Look here for MSDN Online resources.
事实上并没有软驱