c#怎么获取所有局域网中连接的所有设备的名称(不仅仅是计算机名称);
展开全部
//获取所有驱动器
string[] drives = Environment.GetLogicalDrives();
foreach (string driver in drives)
{
Console.WriteLine(driver);
//驱动器信息
DriveInfo di = new DriveInfo(driver);
//是否准备好
Console.WriteLine("isRead:" + di.IsReady);
//名称
Console.WriteLine("name:" + di.Name);
//驱动器根目录
DirectoryInfo root = di.RootDirectory;
RecursiveAsync(root);
Console.WriteLine();
}
Console.ReadKey();
//方法体
private async static void RecursiveAsync(DirectoryInfo root)
{
await Task.Run(() =>
{
try
{
//得到所有文件
FileInfo[] fis = root.GetFiles("*.*");
if (fis != null && fis.Length > 0)
{
foreach (FileInfo fi in fis)
{
//文件名
string name = fi.FullName;
name = name.Length > 100 ? name.Substring(0, 100) + "…" : name;
Console.WriteLine(name);
}
}
//获取子目录
DirectoryInfo[] dis = root.GetDirectories();
//存在子目录
foreach (DirectoryInfo di in dis)
{
//递归子目录
RecursiveAsync(di);
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
});
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询