开发者

How to programmatically get info about storage drives in linux? (C# MONO)

开发者 https://www.devze.com 2022-12-24 02:32 出处:网络
I need programmatically get all storage drives available in a linux system with the following fields:

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 supp开发者_如何学Cort all common storage types: hard disks, Disk-On-Keys, CdRom, Dvd, etc.

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消