我在我的C#项目中使用DriveInfo类来检索给定驱动器上的可用字节.如何正确地将此数字转换为兆字节或千兆字节?除以1024将无法完成我的工作.结果总是与Windows资源管理器中显示的结果不同.
我想在c#中获取可移动磁盘列表.我想跳过本地驱动器.因为我希望用户只将文件保存在可移动磁盘中.
我试图在我的应用程序中列出所有本地驱动器,并DriveInfo.GetDrives
返回本地驱动器号,我也需要映射驱动器.
目前我有:C:,D:和G:(HDD),E:和F:(CD),以及S:和Z :(映射网络驱动器).(是的,它们在Windows资源管理器和Total Commander中都可见.)但是只能以编程方式检索C,D,E,F,G.
我也试过Environment.GetLogicalDrives()
,GetLogicalDriveStrings (pInvoke)
,FindFirstVolume
和FindNextVolumen
(PInvoke的).我试图在注册表中找到映射的驱动器列表.没有任何作用,只有HDD和CD字母被检索.
没有任何异常,直接WinAPI调用中没有指示错误,现在我被卡住了.这是某种安全设置吗?我看到的每个地方,人们都说DriveInfo.GetDrives
应该回放映射的驱动器.这是真的吗?
我正在使用Vista Home Pro.应用程序从本地计算机运行,它也是使用Visual Studio 2008构建的.
我发布了我使用的内容,但它很简单,我不能做错的事情:
foreach (System.IO.DriveInfo di in System.IO.DriveInfo.GetDrives())
Console.WriteLine(di.Name);
Run Code Online (Sandbox Code Playgroud)
结果:C:\ D:\ E:\ F:\ G:\按任意键继续...
我怎样才能使它工作?
我正在开发一个小实用程序,我想更改连接到计算机的闪存驱动器上的卷标.我知道DriveInfo能够做到这一点,但我不知道如何实现它.如果有人有代码示例,我会非常感激.
这是我目前拥有的:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
if (d.IsReady && d.DriveType == DriveType.Removable)
{
//set volume label here
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有卷id的txt文件.
我需要从驱动器卷ID(Windows)获取驱动器信息(驱动器号,驱动器大小等):
卷ID采用以下格式:
\\?\Volume{XXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}
Run Code Online (Sandbox Code Playgroud)
驱动器可以是可移动/本地磁盘
检索信息无关紧要(可能是脚本,cpp,c#,java代码).
编辑:
我试图用DriveInfo,的Win32_LogicalDisk,Win32_Volume,Win32_PnpDevices - 但我找不到这个奇怪的ID ......在所有情况下的ID有differrent格式
更新:
找到了怎么做.
你可以像这样枚举Win32_Volume:
ManagementObjectSearcher ms = new ManagementObjectSearcher("Select * from Win32_Volume");
foreach(ManagementObject mo in ms.Get())
{
var guid = mo["DeviceID"].ToString();
if(guid == myGuid)
return mo["DriveLetter"];
}
Run Code Online (Sandbox Code Playgroud) 我有一个程序告诉我所有的硬盘/ usb,但它只告诉我驱动器号不是名字.这是我有的:
DriveInfo[] drives = DriveInfo.GetDrives();
Console.WriteLine("Detected Drives: ");
for(int i = 0; i < drives.Count(); i++)
{
Console.WriteLine("Drive " + i + ": " + drives[i].Name);
}
return drives;
Run Code Online (Sandbox Code Playgroud)
这打印:
Drive 0: C:\
Drive 1: E:\
Run Code Online (Sandbox Code Playgroud)
但我想要这样的名字
Drive 0: C:\ Local disk
Drive 1: E:\ Kingston USB
Run Code Online (Sandbox Code Playgroud)
我怎么得到这个?
我可以从这篇文章中查看远程连接的PC :使用c-net的远程桌面.但我不需要它.我只需连接该PC并获取C盘的可用空间数据.我怎么能这样做?我可以连接到远程桌面.我可以使用IO命名空间获取driveInfo.但如何将它们结合起来?
如标题中所述,我正在尝试确定驱动器DVD驱动器或CD驱动器.同样,我不是在问媒体,我问的是实际的驱动器本身...... DriveInfo类所指向的对象.
我认为DriveInfo类是不够的,我将不得不从WMI获取Win32_DiskDrive实例并在那里挖掘.问题是到目前为止我发现的所有样本都是关于检测到的介质,但DVD驱动器可以使用CD介质,这是不够的.此外,无论媒体是否存在,代码都应该有效.
我正在创建一个WPF应用程序,除其他外应检查是否存在多个映射驱动器.代码很简单:
DriveInfo[] systemDrives = DriveInfo.GetDrives();
foreach (DriveInfo i in systemDrives)
{
if ((i.Name.Contains("V")) && (i.IsReady))
{
result = true;
break;
}
}
Run Code Online (Sandbox Code Playgroud)
映射的驱动器将映射到所有用户.上面的代码作为普通用户运行时工作正常,但是Visual Studio 2010以管理员身份运行,GetDrives方法仅返回固定驱动器和DVD驱动器,但不返回映射驱动器.如果可执行文件作为管理员运行,则会发生相同的情况 任何想法为什么会这样?
我正在尝试检测我们的应用程序是否从DVD运行(因为这会禁用/启用逻辑中的功能).到目前为止,我已经提出了下面的代码片段似乎可行,但我真的想知道是否有最佳实践来检测这一点.
public static bool IsDVDInstallation()
{
try
{
string location = Assembly.GetExecutingAssembly().Location;
var info = new DriveInfo(Path.GetPathRoot(location));
return info.DriveType == DriveType.CDRom;
}
catch
{
return false;
}
}
Run Code Online (Sandbox Code Playgroud) 我有那个方法来检索NTFS的可移动设备信息:
private void getdriverinfo()
{
// get the usb flash driver
foreach (DriveInfo driveInfo in DriveInfo.GetDrives())
{
if (driveInfo.DriveType == DriveType.Removable && driveInfo.DriveFormat.Equals("NTFS"))
{
comboBox1.Items.Add(driveInfo.Name);
}
}
if (comboBox1.Items.Count == 0)
{
MessageBox.Show("No Removable Device Found , please plug in the USB drive and make sure it is in NTFS format and retry", "No Device Found!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (comboBox1.Items.Count == 1)
{
comboBox1.Text = comboBox1.Items[0].ToString();
}
else
{
MessageBox.Show(comboBox1.Items.Count + " Removable Devices were found , please …
Run Code Online (Sandbox Code Playgroud) 当前我可以使用DriveInfo.GetDrives()在c#中获取所有驱动器及其标签.然后我可以通过这种方法获得分区的磁盘索引/索引.
var searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_DiskPartition");
foreach (var queryObj in searcher.Get())
{
Console.WriteLine("-----------------------------------");
Console.WriteLine("Win32_DiskPartition instance");
Console.WriteLine("Name:{0}", (string)queryObj["Name"]);
Console.WriteLine("Index:{0}", (uint)queryObj["Index"]);
Console.WriteLine("DiskIndex:{0}", (uint)queryObj["DiskIndex"]);
Console.WriteLine("BootPartition:{0}", (bool)queryObj["BootPartition"]);
}
Run Code Online (Sandbox Code Playgroud)
这个问题是DiskIndex,Name和Index基本上只是数字,而不是卷标,即C:\,D:\等......
那么底线如何将DriveInfo上的名称属性卷标作为DiskIndex?要么使用这种方法,要么使用更好的方法.
(这是以下内容:告诉驱动器是分区还是单独的硬盘)
编辑: 我找到Win32_LogicalDisk的管理查询,然后找到Win32_LogicalDiskToPartition.LogicalDisk具有卷,LogicalDisktoParition提供映射.但是,我似乎无法弄清楚如何获取地图.我试着寻找一个JOIN并选择值,但是如果没有在c#代码中进行大量循环,就无法找到关于如何连接的任何内容.
如何编写一个类似这样的检查...如果任何(或至少一个)驱动器类型是CDRom,那么真/继续......否则为假(抛出错误)?
现在,我为每个不通过CDRom要求的驱动器检查抛出了错误.我想我需要使用Any()的LINQ查询,但我不断收到错误.我可能没有正确地写它.
我的计划是在以下情况下抛出错误消息:
- 没有输入CD
- 计算机上没有CD-ROM驱动器
-CD输入为空白
-CD输入不包含所需的特定文件
到目前为止,这是我所希望的方式:
DriveInfo[] allDrives = DriveInfo.GetDrives();
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true && d.DriveType == DriveType.CDRom)
{
DirectoryInfo di = new DirectoryInfo(d.RootDirectory.Name);
var file = di.GetFiles("*.csv", SearchOption.AllDirectories).FirstOrDefault();
if (file == null)
{
errorwindow.Message = LanguageResources.Resource.File_Not_Found;
dialogService.ShowDialog(LanguageResources.Resource.Error, errorWindow);
}
else
{
foreach (FileInfo info in di.GetFiles("*.csv", SearchOption.AllDirectories))
{
Debug.Print(info.FullName);
ImportCSV(info.FullName);
break; // only looking for the first one
}
}
} …
Run Code Online (Sandbox Code Playgroud)