How can I get the partition UUID of a disk in .NET

我的梦境 提交于 2019-12-14 02:13:47

问题


I need to be able to get the partition/filesystem UUID of a partition, similar to how you can with VOL in WinDOS and ls -l /dev/disks/by-partuuid in *nix, but with C# code. What's the best way to do this?

For reference, I need to be able to get the UUID from either the current directory, or the "closest" mount upwards.

EDIT: My bad, should've said this up front: it needs to run on Mono.


回答1:


You can write the code for both platforms and then decide at runtime based on Environment.OSVersion.Platform which one to use. Make sure you write the implementations in separate methods (or classes), that way the runtime won't attempt to load the pinvoked libraries for the wrong platform.

For windows you can use the GetVolumeInformation or the WMI as described in the other answer. For linux you can use the directory you mentioned in your question possibly in conjunction with pinvoking readlink or using the Mono.Posix assembly.




回答2:


You may need to P/Invoke GetVolumeInformation function.



来源:https://stackoverflow.com/questions/18436917/how-can-i-get-the-partition-uuid-of-a-disk-in-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!