C#可以读取盘符,要怎么样才可以显示盘名?
DriveInfo[]dr=DriveInfo.GetDrives();foreach(DriveInfoddindr){if(dd.DriveType==DriveTy...
DriveInfo[] dr = DriveInfo.GetDrives();
foreach (DriveInfo dd in dr)
{
if (dd.DriveType == DriveType.CDRom) //过滤掉是光驱的 磁盘
{
return;
}
else
{
dataGridView1.Rows.Add(dd);
dataGridView1.AllowUserToAddRows = false;
}
}
DriveInfo[] drives = Directory.GetLogicalDrives().Select(q => new DriveInfo(q)).ToArray();
foreach (var driveInfo in drives)
{
if (driveInfo.IsReady)
{
dataGridView1.Rows.Add(driveInfo.VolumeLabel);
}
}
这一个是读取盘名的,两个要怎么整合?有没有哪位大神知道? 展开
foreach (DriveInfo dd in dr)
{
if (dd.DriveType == DriveType.CDRom) //过滤掉是光驱的 磁盘
{
return;
}
else
{
dataGridView1.Rows.Add(dd);
dataGridView1.AllowUserToAddRows = false;
}
}
DriveInfo[] drives = Directory.GetLogicalDrives().Select(q => new DriveInfo(q)).ToArray();
foreach (var driveInfo in drives)
{
if (driveInfo.IsReady)
{
dataGridView1.Rows.Add(driveInfo.VolumeLabel);
}
}
这一个是读取盘名的,两个要怎么整合?有没有哪位大神知道? 展开
1个回答
推荐于2016-11-15 · 知道合伙人软件行家
关注
展开全部
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)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace);
Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace);
Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
foreach (DriveInfo d in allDrives)
{
Console.WriteLine("Drive {0}", d.Name);
Console.WriteLine(" File type: {0}", d.DriveType);
if (d.IsReady == true)
{
Console.WriteLine(" Volume label: {0}", d.VolumeLabel);
Console.WriteLine(" File system: {0}", d.DriveFormat);
Console.WriteLine(
" Available space to current user:{0, 15} bytes",
d.AvailableFreeSpace);
Console.WriteLine(
" Total available space: {0, 15} bytes",
d.TotalFreeSpace);
Console.WriteLine(
" Total size of drive: {0, 15} bytes ",
d.TotalSize);
}
}
更多追问追答
追问
用不了,我是要显示到dataGridView1的,不知道是不是我修改错了
追答
DataTable dt = New DataTable()
dt.Columns.Add(New DataColumn("dName", GetType(String)))
foreach (var driveInfo in drives)
{
if (driveInfo.IsReady)
{
var dr = dt.NewRow();
dr[0]=driveInfo.VolumeLabel;
dt.Rows.Add(dr);
}
}
dataGridView1.datasource=dt;
--winform 不用下面一行,WEB需要下一行代码
dataGridView1.DataBind();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询