-
Json 数据转换成 DataTable
/// <summary> /// Json数据转换成DataTable /// </summary> /// <remark> /// </remark> /// <param name="Json"></param> /// <param name="TableName"></param> /// <returns></returns> public DataTable JsonConvertToDataTable(String TableName, String Json) { DataTable dataTable = new DataTable(); dataTable.TableName = TableName; JArray array = JsonConvert.DeserializeObject(Json) as JArray; if (array!=null && array.Count > 0) { StringBuilder columns = new StringBuilder(); JObject objColumns = array[0] as JObject; //构造表头 foreach (JToken jkon in objColumns.AsEnumerable<JToken>()) { string name = ((JProperty)(jkon)).Name; columns.Append(name + ","); dataTable.Columns.Add(name); } //向表中添加数据 for (int i = 0; i < array.Count; i++) { DataRow row = dataTable.NewRow(); JObject obj = array[i] as JObject; foreach (JToken jkon in obj.AsEnumerable<JToken>()) { string name = ((JProperty)(jkon)).Name; string value = ((JProperty)(jkon)).Value.ToString(); row[name] = value; } dataTable.Rows.Add(row); } } return dataTable; }
-
将 JArray 转换为 datatable
/// <summary> /// 将JArray转换为datatable /// </summary> /// <param name="dataArr"></param> /// <returns></returns> public DataTable JArrayToDatable(JArray dataArr) { if (dataArr == null || dataArr.Count <= 0) return null; DataTable result = new DataTable(); var colnames = ((JObject)dataArr.First).Properties(); List<string> columnNames = new List<string>(); if (colnames == null) return null; foreach (var item in colnames) { if (!columnNames.Contains(item.Name)) { columnNames.Add(item.Name); } result.Columns.Add(item.Name, typeof(string)); } foreach (JObject data in dataArr) { JObject jo = JObject.Parse(data.ToString()); DataRow row = result.NewRow(); foreach (var columnName in columnNames) { if (jo.Property(columnName) == null) { data.Add(columnName, ""); row[columnName] = data[columnName].ToString(); } else { row[columnName] = data[columnName].ToString(); } } result.Rows.Add(row); } return result; }
-
DataTable 使用 Select 方法之后,把结果转换成 DataTable
/// <summary> /// 扩展DataTable Select结果为DataTable /// </summary> /// <remark> /// </remark> public DataTable DataTableSelect(this DataTable me, string where) { if (me == null || me.Rows.Count == 0) return me; var dtNew = me.Clone(); var drArray = me.Select(where); foreach (var item in drArray) dtNew.Rows.Add(item.ItemArray); return dtNew; }
-
.net core 读取 appsettings.json 配置
/// <summary> /// 读取配置信息 /// </summary> /// <remarks> /// </remarks> /// <typeparam name="T"></typeparam> /// <param name="name"></param> /// <returns></returns> public static T Getconfig<T>(string name) where T : class, new() { IConfiguration config = new ConfigurationBuilder() .Add(new JsonConfigurationSource { Path = "appsettings.json", ReloadOnChange = true }) .Build(); var appconfig = new ServiceCollection() .AddOptions() .Configure<T>(config.GetSection(name)) .BuildServiceProvider() .GetService<IOptions<T>>() .Value; return appconfig; }
-
Socket 获取以太网 IP(会筛掉虚拟网卡) 👍
/// <summary> /// 通过socket获取IP /// </summary> /// <returns></returns> public static string SocketGetIp() { string localIP = string.Empty; using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) { socket.Connect("8.8.8.8", 65530); IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint; localIP = endPoint.Address.ToString(); } return localIP; }
-
Dns 获取 IP(只获取 IP V4 的外网 IP) 😄
static void SocketGetIp() { string localIP = string.Empty; using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, 0)) { socket.Connect("8.8.8.8", 65530); IPEndPoint endPoint = socket.LocalEndPoint as IPEndPoint; localIP = endPoint.Address.ToString(); } Console.WriteLine("IP Address = " + localIP); }
-
Network 获取外网 IP 😄
static void NetworkGetIp() {
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface adapter in nics)
{
bool Pd1 = (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet); //判断是否是以太网连接
if (Pd1)
{
Console.WriteLine("网络适配器名称:" + adapter.Name);
Console.WriteLine("网络适配器标识符:" + adapter.Id);
Console.WriteLine("适配器连接状态:" + adapter.OperationalStatus.ToString());
IPInterfaceProperties ip = adapter.GetIPProperties(); //IP配置信息
if (ip.UnicastAddresses.Count > 0)
{
Console.WriteLine("IP地址:" + ip.UnicastAddresses.Where(d => d.IsDnsEligible == true).FirstOrDefault().Address.ToString());
//Console.WriteLine("IP地址:" + ip.UnicastAddresses[0].Address.ToString());
Console.WriteLine("子网掩码:" + ip.UnicastAddresses[0].IPv4Mask.ToString());
}
if (ip.GatewayAddresses.Count > 0)
{
Console.WriteLine("默认网关:" + ip.GatewayAddresses[0].Address.ToString()); //默认网关
}
int DnsCount = ip.DnsAddresses.Count;
Console.WriteLine("DNS服务器地址:"); //默认网关
if (DnsCount > 0)
{
//其中第一个为首选DNS,第二个为备用的,余下的为所有DNS为DNS备用,按使用顺序排列
for (int i = 0; i < DnsCount; i++)
{
Console.WriteLine(" " + ip.DnsAddresses[i].ToString());
}
}
Console.WriteLine("网络接口速度:" + (adapter.Speed / 1000000).ToString("0.0") + "Mbps");
Console.WriteLine("接口描述:" + adapter.Description);
Console.WriteLine("适配器的媒体访问控制 (MAC) 地址:" + adapter.GetPhysicalAddress().ToString());
Console.WriteLine("该接口是否只接收数据包:" + adapter.IsReceiveOnly.ToString());
Console.WriteLine("该接口收到的字节数:" + adapter.GetIPv4Statistics().BytesReceived.ToString());
Console.WriteLine("该接口发送的字节数:" + adapter.GetIPv4Statistics().BytesSent.ToString());
Console.WriteLine("该接口丢弃的传入数据包数:" + adapter.GetIPv4Statistics().IncomingPacketsDiscarded.ToString());
Console.WriteLine("该接口丢弃的传出数据包数:" + adapter.GetIPv4Statistics().OutgoingPacketsDiscarded.ToString());
Console.WriteLine("该接口有错误的传入数据包数:" + adapter.GetIPv4Statistics().IncomingPacketsWithErrors.ToString());
Console.WriteLine("该接口有错误的传出数据包数:" + adapter.GetIPv4Statistics().OutgoingPacketsWithErrors.ToString());
Console.WriteLine("该接口协议未知的数据包数:" + adapter.GetIPv4Statistics().IncomingUnknownProtocolPackets.ToString());
Console.WriteLine("---------------------------------------------------------------------\n");
}
}
}
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于