如何使用C#读取驱动器及其文件夹和文件的数量 [英] How do I read the number of drives and its folder and files using C#
问题描述
使用System;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用SystemInformation;
使用SystemInformation.Properties;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用MySql .Data.MySqlClient;
使用System.Diagnostics;
使用System.IO;
命名空间SystemInformation
{
公共部分类Form1:表格
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender,EventArgs e)
{
试试
{
DriveInfo [] drives = DriveInfo.GetDrives ();
foreach(驱动器中的DriveInfo驱动器)
{
ListViewItem item = new ListViewItem();
Console.WriteLine( drive.Name);
foreach(Directory.EnumerateFiles(drive.Name)中的字符串路径)
{
Console.WriteLine(路径);
lstInfo.Items.Add(item);
}
}
}
catch(MySqlException ex)
{
Trace.WriteLine(ex.Message);
}
catch(例外情况)
{
Trace.WriteLine(ex.Message);
}
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using SystemInformation;
using SystemInformation.Properties;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Diagnostics;
using System.IO;
namespace SystemInformation
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
try
{
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
ListViewItem item = new ListViewItem();
Console.WriteLine(drive.Name);
foreach (string path in Directory.EnumerateFiles(drive.Name))
{
Console.WriteLine(path);
lstInfo.Items.Add(item);
}
}
}
catch (MySqlException ex)
{
Trace.WriteLine(ex.Message);
}
catch (Exception ex)
{
Trace.WriteLine(ex.Message);
}
}
}
}
推荐答案
看看相关MSDN:如何:获取有关文件,文件夹和驱动器的信息(C#编程指南) [ ^ ]
Check out the relevant MSDN: How to: Get Information About Files, Folders, and Drives (C# Programming Guide)[^]
这篇关于如何使用C#读取驱动器及其文件夹和文件的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!