如何在.NET中获取磁盘的分区UUID [英] How can I get the partition UUID of a disk in .NET
问题描述
我需要能够获取分区的分区/文件系统UUID,类似于在WinDOS中使用VOL
在* nix中使用ls -l /dev/disks/by-partuuid
的方式,但是使用C#代码.最好的方法是什么?
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?
作为参考,我需要能够从当前目录或向上的最近"安装中获取UUID.
For reference, I need to be able to get the UUID from either the current directory, or the "closest" mount upwards.
编辑:我不好,我应该提前说一下:它需要在Mono上运行.
My bad, should've said this up front: it needs to run on Mono.
推荐答案
您可以编写两个平台的代码,然后在运行时根据Environment.OSVersion.Platform
决定使用哪个平台.确保将实现分别编写在单独的方法(或类)中,这样运行时就不会尝试为错误的平台加载精心制作的库.
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.
对于Windows,您可以使用GetVolumeInformation或WMI,如其他答案中所述.对于linux,您可以将您在问题中提到的目录与pinvoking readlink或Mono.Posix程序集结合使用.
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.
这篇关于如何在.NET中获取磁盘的分区UUID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!