C# 如何将光驱盘符从 D: 更改为 Z:

发布于 2024-10-18 10:15:44 字数 685 浏览 2 评论 0原文

我正在尝试编写一种方法,将 CDROM 驱动器从字母 D 更改为字母 Z,但对 WMI 没有任何运气。还有其他方法可以使用 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();
        }
    }
}

I am trying to write a metod that will change the CDROM drive from letter D to letter Z and not having any luck with WMI. Is there some other way that 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();
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猫烠⑼条掵仅有一顆心 2024-10-25 10:15:44

我不了解 WMI,但您可以使用 winapi 更改驱动器盘符,这里有一个示例,我将(只是您需要的部分)移植到 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());
}

小心运行此代码,您必须在将驱动器安装点分配给新盘符之前删除驱动器安装点,这可能会导致问题出在原代码上:

/*****************************************************************
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!
*****************************************************************/

I don't know about WMI, but you can change the drive letter with winapi, here is an example that I ported (just the part you need) 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 running this code, you have to delete the drive mount point before assigning it to a new letter, this could lead to problems, from the original 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!
*****************************************************************/
时光沙漏 2024-10-25 10:15:44

杰森,您可以使用 Win32_Volume< /code>

试试这个代码

    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();
    }

jason, you can use the Win32_Volume class

try this code

    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();
    }
忘羡 2024-10-25 10:15:44

谢谢罗德里戈!这正是我所寻找的。我只是在它前面添加了一些 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());
                    }
                }
}

Thanks Rodrigo! That was exactly what I was looking for. I just added some wmi code infront of it so that I could make sure I was grabbing the CDROM drive.

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());
                    }
                }
}
梦途 2024-10-25 10:15:44

我认为 WMI 的 SelectQueries 应该只读取/查询信息,而不执行任何更新。我可能是错的,但我认为要更改驱动器号,您应该在某些 Win32 Api 中降低驱动器号...

I think WMI's SelectQueries are supposed to only read/query information and not to perform any update. I can be wrong but I think to change the drive letter you should go lower in some Win32 Api...

倒数 2024-10-25 10:15:44

请参阅 IOCTL_MOUNTMGR_CREATE_POINT

祝你好运。

唔猫 2024-10-25 10:15:44

我只想在 Rodrigoq 的解决方案中添加一条注释,即在 Vista 及更高版本中,您将需要以提升的权限运行应用程序,否则您将无法删除挂载点并为其分配新的驱动器号,这将导致要抛出的异常。

I would just like to add a note to rodrigoq's solution, that in Vista and up, you will need to run your app with elevated privileges otherwise you will not be able to delete the mount point and assign it a new drive letter which will cause an exception to be thrown.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文