views:

208

answers:

1

I need programmatically get all storage drives available in a linux system with the following fields:

  • Path
  • File System (FAT32, NTFS, etc)
  • Containing Physical Disk

It need to support all common storage types: hard disks, Disk-On-Keys, CdRom, Dvd, etc.

How to do this from C# to run well from MONO?

+1  A: 

It is quite simple. Read the contents of the folder /dev/disk/by-path. The files in this directory are symbolic links to device files in /dev. then you can find some general info about those, using a mono port of gudev - you can get one here. you also need glib-sharp to use this, it is part of the gtk-sharp2 package (install that using your package manager). use as in this example:

GLib.GType.Init();
gudev.GUdevClient a=new gudev.GUdevClient(null);            
gudev.GUdevDevice dev=a.QueryByDeviceFile("/dev/disk/by-path/--some-file--");
Console.WriteLine (dev.GetProperty("ID_FS_TYPE")); //will output the file system, eg. ntfs
Console.WrtieLine(dev.GetProperty("ID_FS_LABEL")); //will output the label of the disk

to get the mountpoint, you should find the actual device file. add a reference to the package "Mono.Posix" and use the Mono.Unix.UnixSymbolicLinkInfo class to find that file. for example:

Mono.Unix.UnixSymbolicLinkInfo sym=new Mono.Unix.UnixSymbolicLinkInfo("/dev/disk/by-path/--some-file--");
Console.WriteLine(sym.ContentsPath); //Will output something like ../../sda1

replace the ../.. with /dev and than read the file /etc/mtab. the format of the file is something like this:

/dev/file mountpoint stuff-you-don't-care-about

Each device in a new line. so split by /n and split that by space. anything after that is simple and i don't think i need to continue.

elad