C# 如何将光驱盘符从 D: 更改为 Z:
我正在尝试编写一种方法,将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我不了解 WMI,但您可以使用 winapi 更改驱动器盘符,这里有一个示例,我将(只是您需要的部分)移植到 C#
小心运行此代码,您必须在将驱动器安装点分配给新盘符之前删除驱动器安装点,这可能会导致问题出在原代码上:
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#
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:
杰森,您可以使用
Win32_Volume< /code>
类
试试这个代码
jason, you can use the
Win32_Volume
classtry this code
谢谢罗德里戈!这正是我所寻找的。我只是在它前面添加了一些 wmi 代码,以便我可以确保我正在获取 CDROM 驱动器。
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.
我认为 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...
请参阅 IOCTL_MOUNTMGR_CREATE_POINT。
祝你好运。
See IOCTL_MOUNTMGR_CREATE_POINT.
Good luck.
我只想在 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.