Resolved Detecting If A Drive Is CD Rom

zunebuggy65

Active member
Joined
Oct 12, 2023
Messages
42
Programming Experience
3-5
I have a save file dialog box. When a user selects a folder to save in, I would like the program to check the drive type and if it is CD type, ask them to select a different drive. It doesn't appear I can detect if it is a writable CD or not, so I'll opt with excluding CD types altogether. Here is one of many things I've tried. I've tried DriveType as well as DriveInfo. The MsgBox statement is just temporary for debugging. I have looked for good examples online but it seems I don't even know what to pass to DriveType and DriveInfo or read it to find out what type of drive the user chose. I keep getting exceptions no matter what I try. I thought the DriveInfo or DriveType would be a string or an integer, but one of the exceptions I got was "could not convert DriveType to String and integer errored out as well.
I don't need to check all the drives, just the drive the user has selected.

VB.NET:
        Dim Drive_Info As System.IO.DriveInfo
        Dim Drive_letter As IO.DriveInfo
        If savFile.ShowDialog() = DialogResult.OK Then
            myFilePath = IO.Path.GetDirectoryName(savFile.FileName)
            myFileFile = IO.Path.GetFileName(savFile.FileName)
            myFNOnly = IO.Path.GetFileNameWithoutExtension(savFile.FileName)
            Drive_letter = IO.DriveInfo.GetDrives(myFilePath)
            Drive_Info = New System.IO.DriveInfo(Drive_letter)
            MsgBox(Drive_Info)
            Return True
        Else
            Return False
        End If

I did declare all the variables shown here for the filenames, they are just declared publicly.

Thank you
 
line 8: you can create DriveInfo directly from savFile.FileName. According to DriveInfo(String) Constructor (System.IO) the driveName parameter can be:
A valid drive letter or fully qualified path.
Then compare DriveType property, start typing If Drive_Info.DriveType = and intellisense give you the options to compare, among them the value DriveType.CDRom
 
Here is what I ended up with and it works, but it sounds like I only need the Path and not the File in Line 7:

VB.NET:
    Private Function SaveFile() As Boolean
        savFile.Filter = "HTML File (*.html, *.htm)|*.html; *.htm"
        If savFile.ShowDialog() = DialogResult.OK Then
            myFilePath = IO.Path.GetDirectoryName(savFile.FileName)
            myFileFile = IO.Path.GetFileName(savFile.FileName)
            myFNOnly = IO.Path.GetFileNameWithoutExtension(savFile.FileName)
            Dim iDriveType As DriveTypes = GetDriveType(IO.Directory.GetDirectoryRoot(myFilePath & "\" & myFileFile))
            If iDriveType = 5 Then
                myResult = MsgBox("This drive is not writable. Please select a different drive.", vbOKOnly + vbCritical, "Drive Type Error")
                myResult = DialogResult.Cancel
            End If
            Return True
        Else
            Return False
        End If
    End Function


    Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Int32
    Public Enum DriveTypes
        Unknown = 0
        Invalid_or_Not_Mounted = 1
        Removable = 2
        Fixed = 3
        Remote = 4
        CDROM = 5
        RAMDisk = 6
    End Enum
 
The DriveType enum is declared in .Net, don't declare a new type yourself. Also never use the numeric value in place of enum values, use the enum value.
VB.NET:
If info.DriveType = DriveType.CDRom Then
 
Back
Top