简体   繁体   English

设置Drive VolumeLabel

[英]Set Drive VolumeLabel

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. 我知道DriveInfo能够做到这一点,但我不知道如何实现它。 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
    }
}

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. 如果只需要更改特定驱动器型号的名称,可以使用WMI的Win32_DiskDrive来缩小范围。

public void SetVolumeLabel(string newLabel)
{
    DriveInfo[] allDrives = DriveInfo.GetDrives();
    foreach (DriveInfo d in allDrives)
    {
        if (d.IsReady && d.DriveType == DriveType.Removable)
        {
            d.VolumeLabel = newLabel;
        }
    }
}

public string VolumeLabel { get; set; }

// Setting the drive name
private void button1_Click(object sender, EventArgs e)
{
    SetVolumeLabel("FlashDrive");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过VolumeLabel从USB获取驱动器号 - Get drive letter from USB via VolumeLabel 如何在C#中获取网络映射的驱动器VolumeLabel? - how to get Network mapped drive VolumeLabel in C#? 如果与应用程序在同一驱动器上,则 FileSystemWatcher 无法使用设置为驱动器根目录的路径 - FileSystemWatcher does not work with path set to drive root if on same drive as application 在运行时将窗口背景设置为驱动器中的图像 - Set Window background as image from drive on runtime PowerShell 设置驱动器标签在重启前保持不变且不可更改 - PowerShell Set Drive Labels Persisting And Unchangeable Until Reboot 在Windows XP上,以编程方式在单个c:驱动器上将Pagefile设置为“No Paging File” - On Windows XP, programmatically set Pagefile to “No Paging File” on single c: drive 如何以编程方式检查/设置对网络驱动器的访问权限? - How to programmatically check/set access permissions to a network drive? 如何在网络驱动器上设置File.Create方法的超时? - How to set a time-out for File.Create method on network drive? 如何在C#中使用Google Drive API为文件设置自定义文件属性 - how to set custom file properties for a file using google drive api in C# 无法使用DataStax C#Drive将cassandra中的“Set”转换为c#中的“SortedSet”或“HashSet” - Unable to cast “Set” in cassandra to “SortedSet” or “HashSet” in c# using DataStax C# Drive
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM