Setting default drive

Trapper
01-06-2003, 03:51 PM
Hello.

In my application I give the user the ability to set certain defaults. One of these involves setting a default drive.

I started out using the DriveListBox but soon ran into problems. The DriveListBox allows the user to select any drive in the DriveListBox they want--even the A: drive when a disk isn't present. However, when the defaults form is revisited, I want to display the current default. If I load up the DriveListBox with an un-ready drive (Drives.Drive="A:"), it checks to see if the drive is ready and throws an error if it isn't. Now, that wouldn't be so bad if I could get it to display the un-ready drive anyway. However, it then displays the C: drive by default. Is there a way to change this behaviour?

I can simply use a ComboBox to display the drives and go that route. However, it is useful for the user to see the *type* of drives listed, i.e. they can see the drive icon. I've thought about using a list box with icons. I can use the GetDriveType api to get the drive's type, but I am at a loss how to discover the icon that goes with that drive type.

Any suggestions?

thanks

shancock
01-07-2003, 04:05 PM
I have attached a mini-app which will demonstrate how to extract an Icon from a "path" device. Simply type the path (C:\,D:\, etc) into the text box on the form and press OK to have the ICON extracted and displayed on the form. This function will turn the Icon into a Picture, as required to add to a ImageList.

I have not put any error checking in this so don't enter a path that doesn't exist! This is for example to help you get started.

This should take care of part two of your question, good luck!

Simon

shancock
01-07-2003, 04:07 PM
Sorry the attachment didn't come through - trying again!

Simon

LiquidPenguin
01-07-2003, 10:43 PM
A while back I wrote part of a module that included available drives and drive status. I wrote the code thinking that I would need it for the module, but ended up never actually using it for that module. It's still there, just extra code that I don't want to get rid of. Go figure :rolleyes:

It's been a while but chances are, I may have forgotten to copy and paste some references, functions or something here. If you try the code and it doesn't work, come back and tell me and I'll locate the missing references. Sometimes, I forget what's really a part of VB and what I custom wrote :)

Mind you, I wrote this and did some debugging, but I never bothered with a refactor. So I'm quite sure there's a number of areas for needed improvement. I just haven't bothered with it. It's also coded in VB 5.0 so if there's specific conflicts with VB 6.0 you'll need to make the adjustments.


Public Function IsDriveMounted(ByVal DriveLetter As String) As Boolean 'Should be refactored
Dim hFindFile As Long 'To hold on to the returning search handle
Dim AFileInfo As WIN32_FIND_DATA 'Structure to hold on to various information about a file
Dim sDriveChar As String 'To hold a single drive letter for proccessing
Dim ErrorHandler As Long 'To deal with various errors
Dim TempErrorLog As ErrorLog
IsDriveMounted = False 'Pre-initialize the output value since there is so many things that can go wrong.
sDriveChar = Left$(DriveLetter, 1) 'Get only the first character in case something corny happens
If IsNumeric(sDriveChar) = False Then 'Only check if the character passed in is actually a character and not a number.
If VerifyDrive(DriveLetter) = True Then 'Verify the drive actually is a drive
sDriveChar = sDriveChar & ":\*.*" 'Add on the appropiate semi-colon and back slash
hFindFile = FindFirstFile(sDriveChar, AFileInfo) 'Get the search handle going and get the name of the first file, if any.
If hFindFile = INVALID_HANDLE_VALUE Then 'Check for an invalid handle, which would occur if the drive is not mounted
ErrorHandler = Err.LastDllError
If ErrorHandler <> ERROR_NOT_READY And ErrorHandler <> ERROR_BAD_NETPATH Then
With TempErrorLog
.InternalFunctionORSub = "Public Function IsDriveMounted(ByVal DriveLetter As String) As Boolean"
.When = Now
.MiscErrorData = "UNKNOWN ERROR VALUE, " & ErrorHandler
End With
ManageErrorLog TempErrorLog
End If
Else
IsDriveMounted = True
End If
End If
End If
FindClose hFindFile
End Function

Private Function VerifyDrive(ByVal CheckPath As String, Optional ByRef DriveType As String) As Boolean 'May need to be refactored.
'Verifies drive letters, Doesn't support double character drives i.e. Drive AA:\
'This does NOT verify if a drive is mounted,
'i.e. if a floppy disk or CD-ROM is in their respective drives.
Dim sDriveLetter As String 'To hold onto the drive letter temporarily
Dim SDriveVal As Long 'To assign a value to a drive letter for filtering
Dim TempErrorLog As ErrorLog 'To hold on to error information

sDriveLetter = UCase(Mid$(CheckPath, 1, 1)) 'Force string into ucase and get the first letter
If sDriveLetter < "A" Or sDriveLetter > "Z" Then GoTo Oh
SDriveVal = 2 ^ (AscW(sDriveLetter) - AscW("A")) 'Derive the ASCII number and bump it up by the power of two to create a filter
If (GetLogicalDrives And SDriveVal) > 0 Then 'Filter the returning Drive code with the resulting filter from above
VerifyDrive = True 'If it is greater than one then return true
SDriveVal = GetDriveType(sDriveLetter & ":\")
Select Case SDriveVal
Case Is = DRIVE_NOIDAVAILABLE '= 0
DriveType = "No ID available"
Case Is = DRIVE_DIRECTORYNOTFOUND '= 1
DriveType = "Specified directory not found"
Case Is = DRIVE_REMOVABLE '= 2
DriveType = "Drive is removable"
Case Is = DRIVE_FIXED '= 3
DriveType = "Drive is a fixed disk"
Case Is = DRIVE_REMOTE '= 4
DriveType = "Drive is a network drive"
Case Is = DRIVE_CDROM '= 5
DriveType = "Drive is a CD-ROM"
Case Is = DRIVE_RAMDISK '= 6
DriveType = "Drive is a RAM disk"
Case Else 'For anything else
DriveType = "Unknown"
With TempErrorLog
.InternalFunctionORSub = "Private Function VerifyDrive(ByVal CheckPath As String, Optional ByRef DriveType As String) As Boolean"
.MiscErrorData = "The drive type is an unknown type of drive"
.When = Now
End With
ManageErrorLog TempErrorLog
End Select
Else
Oh :
VerifyDrive = False 'otherwise return false
DriveType = "Invalid drive letter"
End If
End Function

Trapper
01-08-2003, 10:10 AM
Those are both great ideas. Thanks. I'll post back when I've given them a try.

It appears that the DriveListBox remains an enigma.

Here's another question:

I've used the SHBrowseForFolder and seen it mentioned in the forum several times. Is it possible to display it in a drop-down box instead of its own dialog?

I would bet you could hook a combobox and, using the hWnd of the dialog, display it as the dropdown. Be an interesting project to work on.

Trapper

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum