因为现场的设备ip配置都是固定的,有时候想测试网络怎么样要挨个ping特麻烦,就弄了个方法来ping,文件路径暂时都是写死的
.net core 的 一个控制台程序
1 static void Main(string[] args) 2 { 3 Console.WriteLine("Hello World!"); 4 DataSet ds = ToDataTable("F:\\***\\***\\IP地址分配.xlsx"); 5 DataTable dt = ds.Tables[0]; 6 string butrong = string.Empty; 7 for (int i = 0; i < dt.Rows.Count; i++) 8 { 9 string ip = dt.Rows[i][3].ToString().Trim().Replace("。", "."); 10 if (!string.IsNullOrEmpty(ip)) 11 { 12 string rtn = (PingIp(ip) ? "trun" : "false"); 13 if (rtn == "false") 14 { 15 butrong += dt.Rows[i][2].ToString(); 16 butrong += ip; 17 butrong += "||"; 18 } 19 Console.WriteLine("pingIP" + ip+" : "+ rtn); 20 } 21 22 } 23 Console.WriteLine("不通的ip有:"+ butrong); 24 } 25 /// <summary> 26 /// ping ip,测试能否ping通 27 /// </summary> 28 /// <param name="strIP">IP地址</param> 29 /// <returns></returns> 30 private static bool PingIp(string strIP) 31 { 32 bool bRet = false; 33 try 34 { 35 Ping pingSend = new Ping(); 36 PingReply reply = pingSend.Send(strIP, 1000); 37 if (reply.Status == IPStatus.Success) 38 bRet = true; 39 } 40 catch (Exception) 41 { 42 bRet = false; 43 } 44 return bRet; 45 } 46 47 48 49 /// <summary> 50 /// 读取Excel文件到DataSet中 51 /// </summary> 52 /// <param name="filePath">文件路径</param> 53 /// <returns></returns> 54 public static DataSet ToDataTable(string filePath) 55 { 56 string connStr = ""; 57 string fileType = System.IO.Path.GetExtension("F:\\***\\***\\IP地址分配.xlsx"); 58 if (string.IsNullOrEmpty(fileType)) return null; 59 60 if (fileType == ".xls") 61 connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\""; 62 else 63 connStr = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + filePath + ";" + ";Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\""; 64 string sql_F = "Select * FROM [{0}]"; 65 66 OleDbConnection conn = null; 67 OleDbDataAdapter da = null; 68 DataTable dtSheetName = null; 69 70 DataSet ds = new DataSet(); 71 try 72 { 73 // 初始化连接,并打开 74 conn = new OleDbConnection(connStr); 75 conn.Open(); 76 77 // 获取数据源的表定义元数据 78 string SheetName = ""; 79 dtSheetName = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" }); 80 81 // 初始化适配器 82 da = new OleDbDataAdapter(); 83 for (int i = 0; i < dtSheetName.Rows.Count; i++) 84 { 85 SheetName = (string)dtSheetName.Rows[i]["TABLE_NAME"]; 86 87 if (SheetName.Contains("$") && !SheetName.Replace("‘", "").EndsWith("$")) 88 { 89 continue; 90 } 91 92 da.SelectCommand = new OleDbCommand(String.Format(sql_F, SheetName), conn); 93 DataSet dsItem = new DataSet(); 94 da.Fill(dsItem,i.ToString());//这里找个第二个参数还没弄明白是干嘛用的,网上抄的 95 96 ds.Tables.Add(dsItem.Tables[0].Copy()); 97 } 98 } 99 catch (Exception ex) 100 { 101 } 102 finally 103 { 104 // 关闭连接 105 if (conn.State == ConnectionState.Open) 106 { 107 conn.Close(); 108 da.Dispose(); 109 conn.Dispose(); 110 } 111 } 112 return ds; 113 }