c#怎么获取所有局域网中连接的所有设备的名称(不仅仅是计算机名称);

2025-04-01 00:40:07
推荐回答(2个)
回答1:

//获取所有驱动器  
           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);  
               }  
           });  
       }

回答2:

建议你看看SNMP协议,应该有帮助