Anonymous585191

I just need a code that find the path (letter) of CD-ROM drive in any PC. For example I have published my program into a CD and in my program codes I want to open a file which is on the CD where my setup files are. Please help me to make it. THANK YOU ALREADY



Re: Visual Basic Express Edition How to define a code to find CD-ROM drive or letter in any PC

James Bragg

Hey Anon,

I found a code snippet in the help files I made some slight changes to get it running.

Open a new console app and cut and paste the code in.

Module Module1

Public Sub Main()

Dim allDrives() As IO.DriveInfo = IO.DriveInfo.GetDrives()

Dim d As IO.DriveInfo

For Each d In allDrives

Console.WriteLine("Drive {0}", d.Name)

Console.WriteLine(" File type: {0}", d.DriveType)

If d.IsReady = True Then

Console.WriteLine(" Volume label: {0}", d.VolumeLabel)

Console.WriteLine(" File system: {0}", d.DriveFormat)

Console.WriteLine( _

" Available space to current user:{0, 15} bytes", _

d.AvailableFreeSpace)

Console.WriteLine( _

" Total available space: {0, 15} bytes", _

d.TotalFreeSpace)

Console.WriteLine( _

" Total size of drive: {0, 15} bytes ", _

d.TotalSize)

End If

Next

Console.WriteLine("Press any key to exit")

Console.ReadKey()

End Sub

End Module

Hope this helps






Re: Visual Basic Express Edition How to define a code to find CD-ROM drive or letter in any PC

spotty

Modified to simply return the first CDRom Drive Root Path

Code Snippet

Module Module1

Public Sub Main()
MsgBox(ReturnFirstCDRomName)
Console.WriteLine("Press any key to exit")
Console.ReadKey()

End Sub

Function ReturnFirstCDRomName() As String
Dim sDrive As String = ""
For Each d As IO.DriveInfo In IO.DriveInfo.GetDrives()
If d.DriveType = IO.DriveType.CDRom Then
sDrive = d.Name
Exit For
End If
Next
Return sDrive
End Function


End Module





Re: Visual Basic Express Edition How to define a code to find CD-ROM drive or letter in any PC

James Bragg

Hey Spotty,

I like the function! Great idea!






Re: Visual Basic Express Edition How to define a code to find CD-ROM drive or letter in any PC

spotty

it should really return a list of letters as more than 1 CD may be present but thats easy enough to change.





Re: Visual Basic Express Edition How to define a code to find CD-ROM drive or letter in any PC

HanneSThEGreaT

I know this has been superbly answered by spotty and James, but there is another way .

using the GetDriveType and GetLogicalDriveStrings APIs

Code Snippet

Imports System.Runtime.InteropServices 'Use with DllImport

Public Class Form1

<DllImport("kernel32.dll", EntryPoint:="GetDriveType", SetLastError:=True, CharSet:=CharSet.Auto)> _

Private Shared Function GetDriveType(ByVal nDrive As String) As Int32

End Function 'Get Drive Type

<DllImport("kernel32.dll", EntryPoint:="GetLogicalDriveStrings", SetLastError:=True, CharSet:=CharSet.Auto)> _

Private Shared Function GetLogicalDriveStrings(ByVal nBufferLength As Int32, ByVal lpBuffer As String) As Int32

End Function 'Get Drive Letter

Private Const DRIVE_CDROM As Integer = 5 'CD rom = 5

Private strDriveLetter As String 'Holds Drive Letter

Private Function GetFirstCdRomDriveLetter() As String

' Declare variables.

Dim lDriveType As Integer

Dim strDrive As String

Dim lStart As Integer

lStart = 1

' Create a string to hold the logical drives.

Dim strDrives As String

strDrives = Space(150)

' Get the logial drives on the system.

' If the function fails it returns zero.

Dim lRetVal As Integer

lRetVal = GetLogicalDriveStrings(150, strDrives)

' Check to see if GetLogicalDriveStrings() worked.

If lRetVal = 0 Then

' Get GetLogicalDriveStrings() failed.

GetFirstCdRomDriveLetter = String.Empty

Exit Function

End If

' Get the string that represents the first drive.

strDrive = Mid(strDrives, lStart, 3)

Do

' Test the first drive.

lDriveType = GetDriveType(strDrive)

' Check if the drive type is a CD-ROM.

If lDriveType = DRIVE_CDROM Then

' Found the first CD-ROM drive on the system.

GetFirstCdRomDriveLetter = strDrive

Exit Function

End If

' Increment lStart to next drive in the string.

lStart = lStart + 4

' Get the string that represents the first drive.

strDrive = Mid(strDrives, lStart, 3)

Loop While (Mid(strDrives, lStart, 1) <> vbNullChar)

End Function

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' Call the GetFirstCdRomDriveLetter() and store the

' return value in strDriveLetter.

strDriveLetter = GetFirstCdRomDriveLetter()

Select Case strDriveLetter

Case "a:\"

strDriveLetter = "a:"

Case "b:\"

strDriveLetter = "b:"

Case "c:\"

strDriveLetter = "c:"

Case "d:\"

strDriveLetter = "d:"

Case "e:\"

strDriveLetter = "e:"

Case "f:\"

strDriveLetter = "f:"

Case "g:\"

strDriveLetter = "g:"

Case "h:\"

strDriveLetter = "h:"

Case "i:\"

strDriveLetter = "i:"

Case "j:\"

strDriveLetter = "j:"

End Select

MessageBox.Show(strDriveLetter)

End Sub

End Class