我想为用户显示一个打开的对话框,用户可以在其中仅选择系统中可移动磁盘中的文件。我正在使用下面的代码。但我需要获取可移动磁盘的Guid才能打开对话框。
告诉我怎么..。
System.Windows.Forms.OpenFileDialog dls = new System.Windows.Forms.OpenFileDialog();
dls.CustomPlaces.Clear();
foreach (DriveInfo Drive in ListDrives)
{
if (Drive.DriveType == DriveType.Removable)
{
dls.CustomPlaces.Add(-----Guid of Drive------);
}
dls.ShowDialog();
}发布于 2009-07-15 02:23:34
我不知道如何获取可移动驱动器的Guids,但是设置CustomPlaces集合不会限制您的用户选择除了集合中的文件夹之外的任何内容,它们将只是在打开文件对话框的左侧显示为快捷方式,然后只有当您的AutoUpgradeEnabled属性设置为true并且您的用户正在运行Windows Vista或更高版本时。
请参阅:http://msdn.microsoft.com/en-us/library/bb397814.aspx
不过,要结束您开始的位置,请使用DriveInfo.Name在自定义位置中创建一个新条目:
System.Windows.Forms.OpenFileDialog dls = new System.Windows.Forms.OpenFileDialog();
dls.CustomPlaces.Clear();
foreach (DriveInfo Drive in ListDrives)
{
if (Drive.DriveType == DriveType.Removable)
{
dls.CustomPlaces.Add(Drive.Name);
}
dls.ShowDialog();
}发布于 2009-07-15 02:39:14
Karthik,一个更好的方法是获取所选文件的路径,并检查它是否来自可移动驱动器。
OpenFileDialog ofd = new OpenFileDialog();
ofd.CustomPlaces.Clear();
foreach (var item in System.IO.DriveInfo.GetDrives())
{
if (item.DriveType == DriveType.Removable)
ofd.CustomPlaces.Add(item.RootDirectory.ToString());
}
if (ofd.ShowDialog() == DialogResult.OK)
{
FileInfo f = new FileInfo(ofd.FileName);
string s = f.Directory.Root.ToString();
DriveInfo df = new DriveInfo(s);
if (df.DriveType == DriveType.Removable)
{
//DO STUFF WITH FILE
}
}发布于 2009-07-15 02:41:36
WMI通过Win32_DiskDrive类的DeviceID属性提供对磁盘驱动器的GUID的访问。
使用ManagementObjectSearcher或文档中描述的其他查询方法之一执行WMI查询。
因为我现在还不能访问VS,所以我不能提供代码示例,尽管我确信MSDM有一个。
https://stackoverflow.com/questions/1129071
复制相似问题