设置驱动器卷标签
我正在开发一个小型实用程序,我想更改连接到计算机的闪存驱动器上的卷标。我知道 DriveInfo 能够做到这一点,但我不知道如何实现它。如果有人有代码示例,我将非常感激。
这是我目前拥有的:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady && d.DriveType == DriveType.Removable)
{
//set volume label here
}
}
I am working on a small utility where I would like to change the volume label on flash drives that are connected to the computer. I know that DriveInfo is capable of doing it but I am at a loss as for how to accomplish it. If anyone has a code sample I would really appreciate it.
Here is what i currently have:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady && d.DriveType == DriveType.Removable)
{
//set volume label here
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
谢谢詹姆斯!我不知道为什么我有这么多问题,但你让我走上了正确的道路。
这是设置驱动器标签的最终代码。对于使用此功能的其他人,它将更改连接到系统的任何可移动驱动器的名称。如果您只需要更改特定驱动器型号的名称,可以使用 WMI 的
Win32_DiskDrive
来缩小范围。Thanks James! I don't know why I had so many issues with this but you got me going down the right path.
Here is the final code to set the drive label. For anyone else that uses this, it will change the name of ANY removable drive that is attached to the system. If you need to only change the names of specific drive models you can use WMI's
Win32_DiskDrive
to narrow it down.您尝试过 DriveInfo.VolumeLabel 吗?
http://msdn.microsoft.com/en-us /library/system.io.driveinfo.volumelabel.aspx
Have you tried DriveInfo.VolumeLabel?
http://msdn.microsoft.com/en-us/library/system.io.driveinfo.volumelabel.aspx