C # How to change the drive letter of a CDROM from D: to Z:

I am trying to write a method that changes a CDROM from letter D to letter Z and is not lucky with WMI. Is there any other way I can do this using C #?

public void setVolCDROM()
{
    SelectQuery queryCDROM = 
        new SelectQuery("SELECT * FROM Win32_cdromdrive");
    ManagementObjectSearcher searcherCDROM = 
        new ManagementObjectSearcher(queryCDROM);
    foreach(ManagementObject cdromLetter in searcherCDROM.Get())
    {
        MessageBox.Show(cdromLetter["Drive"].ToString() + "\n"
            + cdromLetter["Manufacturer"].ToString());
        if (cdromLetter["Drive"].ToString() == "D:")
        {
            cdromLetter["Drive"] = "Z:";                        
            cdromLetter.Put();
        }
    }
}
+5
source share
6 answers

I do not know about WMI, but you can change the drive letter using winapi, here is an example that I ported (you just need the part) to C #

[DllImport("kernel32.dll", SetLastError = true)]
static extern bool GetVolumeNameForVolumeMountPoint(string
    lpszVolumeMountPoint, [Out] StringBuilder lpszVolumeName,
    uint cchBufferLength);

[DllImport("kernel32.dll")]
static extern bool DeleteVolumeMountPoint(string lpszVolumeMountPoint);

[DllImport("kernel32.dll")]
static extern bool SetVolumeMountPoint(string lpszVolumeMountPoint,
    string lpszVolumeName);

const int MAX_PATH = 260;

private void ChangeDriveLetter()
{
    StringBuilder volume = new StringBuilder(MAX_PATH);
    if (!GetVolumeNameForVolumeMountPoint(@"D:\", volume, (uint)MAX_PATH))
        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());

    if (!DeleteVolumeMountPoint(@"D:\"))
        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());

    if (!SetVolumeMountPoint(@"Z:\", volume.ToString()))
        Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
}

Be careful when running this code, you must remove the mount point of the drive before assigning a new letter, which can lead to problems with the source code:

/*****************************************************************
WARNING WARNING WARNING WARNING WARNING WARNING WARNING WARNING  

   This program will change drive letter assignments, and the    
   changes persist through reboots. Do not remove drive letters  
   of your hard disks if you do not have this program on floppy  
   disk or you might not be able to access your hard disks again!
*****************************************************************/
+6
source

jason you can use Win32_Volumeclass

    ManagementObjectSearcher disks = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_Volume WHERE DriveLetter = 'D:'");
    foreach (ManagementObject disk in disks.Get())
    {
        disk.Get();
        disk["DriveLetter"] = "Z:";
        disk.Put();
    }
+1

, WMI SelectQueries / , - . , , , Win32 Api...

0

! , . wmi , , CDROM.

public void setCDROM(){
                SelectQuery queryCDROM =
                        new SelectQuery("SELECT * FROM Win32_cdromdrive");
                ManagementObjectSearcher searcherCDROM =
                        new ManagementObjectSearcher(queryCDROM);
                int i = 0;
                foreach(ManagementObject cdromLetter in searcherCDROM.Get())
                {
                    // if stement in place to handle if there is more than one cdrom drive
                    // this will only handle the first cdrom drive encountered 
                    i = i + 1;
                    if (i == 1)
                    {
                        // run the ChangeDriveLetter method passing the drive letter string
                        ChangeDriveLetter(cdromLetter["Drive"].ToString());
                    }
                }
}
0

I would like to add a note to the rodrigoq solution that in Vista and above you will need to run the application with elevated privileges, otherwise you will not be able to remove the mount point and assign it a new drive letter that will throw an exception.

0
source

All Articles