using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Net.NetworkInformation;
|
using System.Net.Sockets;
|
using System.Windows.Forms;
|
using System.IO;
|
using System.IO.Compression;
|
|
namespace BatchService.Framework.Utility
|
{
|
public static class Common
|
{
|
public static int GetAge(string i_age)
|
{
|
int t_age = 0;
|
|
switch (i_age.Substring(i_age.Length - 1, 1))
|
{
|
case "天":
|
case "月":
|
case "岁":
|
int.TryParse(i_age.Substring(0, i_age.Length - 1), out t_age);
|
break;
|
}
|
|
return t_age;
|
}
|
|
public static string GetAgeUnit(string i_age)
|
{
|
return i_age.Substring(i_age.Length - 1, 1);
|
}
|
|
public static double ConvertPriceStringToDouble(string i_price)
|
{
|
double t_db_price = 0;
|
|
double.TryParse(i_price, out t_db_price);
|
|
return t_db_price;
|
}
|
|
/// <summary>
|
/// 时间戳转为C#格式时间
|
/// </summary>
|
/// <param name="timeStamp">Unix时间戳格式</param>
|
/// <returns>C#格式时间</returns>
|
public static DateTime GetTime(string timeStamp)
|
{
|
DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
|
long lTime = long.Parse(timeStamp + "0000000");
|
TimeSpan toNow = new TimeSpan(lTime);
|
return dtStart.Add(toNow);
|
}
|
|
|
/// <summary>
|
/// DateTime时间格式转换为Unix时间戳格式
|
/// </summary>
|
/// <param name="time"> DateTime时间格式</param>
|
/// <returns>Unix时间戳格式</returns>
|
public static int ConvertDateTimeInt(System.DateTime time)
|
{
|
System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));
|
return (int)(time - startTime).TotalSeconds;
|
}
|
|
/// <summary>
|
/// 是否数字
|
/// </summary>
|
/// <param name="message"></param>
|
/// <param name="result"></param>
|
/// <returns></returns>
|
public static bool isNumberic(string message, out int result)
|
{
|
System.Text.RegularExpressions.Regex rex =
|
new System.Text.RegularExpressions.Regex(@"^\d+$");
|
result = -1;
|
if (rex.IsMatch(message))
|
{
|
result = int.Parse(message);
|
return true;
|
}
|
else
|
return false;
|
}
|
|
public static string GetLocalIP()
|
{
|
List<string> strIPs = new List<string>();
|
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
|
foreach (NetworkInterface adapter in nics)
|
{
|
|
if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
|
{
|
var mac = adapter.GetPhysicalAddress(); Console.WriteLine(mac);
|
IPInterfaceProperties ip = adapter.GetIPProperties();
|
UnicastIPAddressInformationCollection ipCollection = ip.UnicastAddresses;
|
foreach (UnicastIPAddressInformation ipadd in ipCollection)
|
{
|
//InterNetwork IPV4地址
|
//InterNetworkV6 IPV6地址
|
if (ipadd.Address.AddressFamily == AddressFamily.InterNetwork)
|
{ //判断是否为ipv4
|
Console.WriteLine(ipadd.Address.ToString());
|
strIPs.Add(ipadd.Address.ToString());
|
}
|
}
|
}
|
}
|
return string.Join(";", strIPs);
|
}
|
|
/// <summary>
|
/// 压缩数据
|
/// </summary>
|
/// <param name="data"></param>
|
/// <returns></returns>
|
public static byte[] Compress(byte[] data)
|
{
|
MemoryStream ms = new MemoryStream();
|
GZipStream zipStream = new GZipStream(ms, CompressionMode.Compress);
|
zipStream.Write(data, 0, data.Length);//将数据压缩并写到基础流中
|
zipStream.Close();
|
return ms.ToArray();
|
}
|
|
/// 解压数据
|
/// </summary>
|
/// <param name="data"></param>
|
/// <returns></returns>
|
public static byte[] Decompress(byte[] data)
|
{
|
MemoryStream srcMs = new MemoryStream(data);
|
GZipStream zipStream = new GZipStream(srcMs, CompressionMode.Decompress);
|
MemoryStream ms = new MemoryStream();
|
byte[] bytes = new byte[40960];
|
int n;
|
while ((n = zipStream.Read(bytes, 0, bytes.Length)) > 0)
|
{
|
ms.Write(bytes, 0, n);
|
}
|
zipStream.Close();
|
return ms.ToArray();
|
}
|
|
public static byte[] ToByteArray(this Stream stream)
|
{
|
stream.Position = 0;
|
byte[] buffer = new byte[stream.Length];
|
for (int totalBytesCopied = 0; totalBytesCopied < stream.Length; )
|
totalBytesCopied += stream.Read(buffer, totalBytesCopied, Convert.ToInt32(stream.Length) - totalBytesCopied);
|
return buffer;
|
}
|
|
}
|
}
|