PDA

View Full Version : Cross-platform way of getting list of all drives (A - Z) on a computer?



paul_nicholls
25-07-2011, 02:31 AM
Hi all,
I was wondering if there is a cross-platform way of getting a list of all drives installed in a computer?

I know of GetLogicalDriveStrings which returns a list of drive letters, but I am not sure if this is windows only...

EDIT: How would one do this on a Linux/Mac OSX machine? Those OSs don't use drive letters like Windows, do they?

cheers,
Paul

paul_nicholls
25-07-2011, 05:30 AM
Actually, on this topic - I want to create a simple form in Lazarus that will allow the user to select a drive, then a folder on that drive, and then show all the files in the selected folder.

I am trying to make a sound preview form that the user can browse for sounds, play one to see if they like it, and then return that filename to the parent form.

It seems rather easy to do and I could just re-invent the wheel, but thought there might be one already done that I can modify :)

EDIT: and I would still like to do this in a cross-platform manner if possible :)

cheers,
Paul

paul_nicholls
25-07-2011, 07:01 AM
I just looked at how GIMP does it and that seems pretty good :)
http://img84.imageshack.us/img84/3276/gimpfileopen.png (http://imageshack.us/photo/my-images/84/gimpfileopen.png/)

The Location edit control also directly controls which driver/folder you are - you start typing in that, and it fills with the closest match to date until you select one and press enter to update the files list below...

It would be great if I could emulate this or do something similar!

I guess Linux/Mac OSX would have just folder names or similar in the places list instead of drive letters?

cheers,
Paul

User137
25-07-2011, 10:56 AM
As far as i know, Windows is the only OS that uses drive letters. In Linux DVD and other drives are something like /mnt/.../ i don't even remember too well.

paul_nicholls
25-07-2011, 11:51 AM
As far as i know, Windows is the only OS that uses drive letters. In Linux DVD and other drives are something like /mnt/.../ i don't even remember too well.

You are right - I remembered the Linux and /mnt, etc. after I did the initial post :D

So, I don't suppose you (or anyone else) would have any idea where to start looking to get Linux/Mac OSX drive names on a machine then?

cheers,
Paul

Legolas
25-07-2011, 01:57 PM
Could it be only matter of listing the subdirectories in /mnt?

de_jean_7777
25-07-2011, 02:17 PM
Some temporary drives (usb memory sticks, ...) are also mounter under /media/.

Stoney
25-07-2011, 04:56 PM
Mac OS X usually mounts drives under /Volumes/.

paul_nicholls
25-07-2011, 08:48 PM
Thanks all, I will see what I can come up with :)

cheers,
Paul

Cybermonkey
26-07-2011, 09:14 AM
Hm, I not sure if I understood correctly. You want the User first select the drive and then the file, right? But what about the ordinary TopenDialog? One can select all drives from within ... and this works on all OSs. But maybe I got something wrong ...

503

paul_nicholls
26-07-2011, 10:33 AM
Hm, I not sure if I understood correctly. You want the User first select the drive and then the file, right? But what about the ordinary TopenDialog? One can select all drives from within ... and this works on all OSs. But maybe I got something wrong ...

503

Thanks Cybermonkey, but if I use the standard TOpenDialog, I need to close that dialog before I can let the user play a selected sound file...

If I use a custom one, I can let the user play a selected sound and then if they are happy, close the dialog box, returning the file name :)

cheers,
Paul

pstudio
26-07-2011, 11:55 AM
Then how about extending TOpenDialog to have a preview function?

paul_nicholls
26-07-2011, 12:06 PM
hmm...interesting idea, thanks!

I wonder how easy it would be to do under Lazarus? I am more familiar with Delphi controls :)

cheers,
Paul

Ingemar
27-07-2011, 06:25 PM
I can think of a few ways to handle it.

- Specify drives with a string. Under Windows, it will be a single letter string. Under Unix, it will be the name of the volume. (Although Unix hides volumes, it is really just a directory in /Volumes etc.)
- Index all volumes into an array, and index it using the drive letter. (Makes everything look more like Windows to you.)

Of course, you must also handle path formats, using "\" or "/" and making it start right. There are quite a few things to handle, and making that transparent is a challenge.

When I made a Windows port of an application (from Mac) I created full path strings in the file dialogs, and used that. As long as I didn't mess with the string contents, this was transparent. It is convenient to use a separate string for the file name without the path though. Also, on the Mac it isn't recommended to use path name strings, because the OS will handle files moved while in use (very elegant) but it can't change your own strings.

paul_nicholls
29-07-2011, 11:42 AM
Thanks for the tips Ingemar :)

cheers,
Paul