feat:上线服务监控功能
This commit is contained in:
@@ -0,0 +1,446 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
#region 转换为long
|
||||
/// <summary>
|
||||
/// 将object转换为long,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static long ParseToLong(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return long.Parse(obj.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0L;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为long,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static long ParseToLong(this string str, long defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return long.Parse(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为int
|
||||
/// <summary>
|
||||
/// 将object转换为int,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static int ParseToInt(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return Convert.ToInt32(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为int,若转换失败,则返回指定值。不抛出异常。
|
||||
/// null返回默认值
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static int ParseToInt(this object str, int defaultValue)
|
||||
{
|
||||
if (str == null)
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
try
|
||||
{
|
||||
return Convert.ToInt32(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为short
|
||||
/// <summary>
|
||||
/// 将object转换为short,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static short ParseToShort(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return short.Parse(obj.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为short,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static short ParseToShort(this object str, short defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return short.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为demical
|
||||
/// <summary>
|
||||
/// 将object转换为demical,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal ParseToDecimal(this object str, decimal defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return decimal.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为demical,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal ParseToDecimal(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return decimal.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转化为bool
|
||||
/// <summary>
|
||||
/// 将object转换为bool,若转换失败,则返回false。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ParseToBool(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return bool.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为bool,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool ParseToBool(this object str, bool result)
|
||||
{
|
||||
try
|
||||
{
|
||||
return bool.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为float
|
||||
/// <summary>
|
||||
/// 将object转换为float,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static float ParseToFloat(this object str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return float.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为float,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static float ParseToFloat(this object str, float result)
|
||||
{
|
||||
try
|
||||
{
|
||||
return float.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return result;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为Guid
|
||||
/// <summary>
|
||||
/// 将string转换为Guid,若转换失败,则返回Guid.Empty。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static Guid ParseToGuid(this string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new Guid(str);
|
||||
}
|
||||
catch
|
||||
{
|
||||
return Guid.Empty;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为DateTime
|
||||
/// <summary>
|
||||
/// 将string转换为DateTime,若转换失败,则返回日期最小值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime ParseToDateTime(this string str)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
if (str.Contains("-") || str.Contains("/"))
|
||||
{
|
||||
return DateTime.Parse(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
int length = str.Length;
|
||||
switch (length)
|
||||
{
|
||||
case 4:
|
||||
return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 6:
|
||||
return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 8:
|
||||
return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 10:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 12:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 14:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
default:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return DateTime.MinValue;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将string转换为DateTime,若转换失败,则返回默认值。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime ParseToDateTime(this string str, DateTime? defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(str))
|
||||
{
|
||||
return defaultValue.GetValueOrDefault();
|
||||
}
|
||||
if (str.Contains("-") || str.Contains("/"))
|
||||
{
|
||||
return DateTime.Parse(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
int length = str.Length;
|
||||
switch (length)
|
||||
{
|
||||
case 4:
|
||||
return DateTime.ParseExact(str, "yyyy", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 6:
|
||||
return DateTime.ParseExact(str, "yyyyMM", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 8:
|
||||
return DateTime.ParseExact(str, "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 10:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHH", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 12:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmm", System.Globalization.CultureInfo.CurrentCulture);
|
||||
case 14:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
default:
|
||||
return DateTime.ParseExact(str, "yyyyMMddHHmmss", System.Globalization.CultureInfo.CurrentCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue.GetValueOrDefault();
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为string
|
||||
/// <summary>
|
||||
/// 将object转换为string,若转换失败,则返回""。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string ParseToString(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (obj == null)
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj.ToString();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
public static string ParseToStrings<T>(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
var list = obj as IEnumerable<T>;
|
||||
if (list != null)
|
||||
{
|
||||
return string.Join(",", list);
|
||||
}
|
||||
else
|
||||
{
|
||||
return obj.ToString();
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 转换为double
|
||||
/// <summary>
|
||||
/// 将object转换为double,若转换失败,则返回0。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="obj"></param>
|
||||
/// <returns></returns>
|
||||
public static double ParseToDouble(this object obj)
|
||||
{
|
||||
try
|
||||
{
|
||||
return double.Parse(obj.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将object转换为double,若转换失败,则返回指定值。不抛出异常。
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="defaultValue"></param>
|
||||
/// <returns></returns>
|
||||
public static double ParseToDouble(this object str, double defaultValue)
|
||||
{
|
||||
try
|
||||
{
|
||||
return double.Parse(str.ToString());
|
||||
}
|
||||
catch
|
||||
{
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 强制转换类型
|
||||
/// <summary>
|
||||
/// 强制转换类型
|
||||
/// </summary>
|
||||
/// <typeparam name="TResult"></typeparam>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<TResult> CastSuper<TResult>(this IEnumerable source)
|
||||
{
|
||||
foreach (object item in source)
|
||||
{
|
||||
yield return (TResult)Convert.ChangeType(item, typeof(TResult));
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
//using Newtonsoft.Json;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
#region 枚举成员转成dictionary类型
|
||||
/// <summary>
|
||||
/// 转成dictionary类型
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<int, string> EnumToDictionary(this Type enumType)
|
||||
{
|
||||
Dictionary<int, string> dictionary = new Dictionary<int, string>();
|
||||
Type typeDescription = typeof(DescriptionAttribute);
|
||||
FieldInfo[] fields = enumType.GetFields();
|
||||
int sValue = 0;
|
||||
string sText = string.Empty;
|
||||
foreach (FieldInfo field in fields)
|
||||
{
|
||||
if (field.FieldType.IsEnum)
|
||||
{
|
||||
sValue = ((int)enumType.InvokeMember(field.Name, BindingFlags.GetField, null, null, null));
|
||||
object[] arr = field.GetCustomAttributes(typeDescription, true);
|
||||
if (arr.Length > 0)
|
||||
{
|
||||
DescriptionAttribute da = (DescriptionAttribute)arr[0];
|
||||
sText = da.Description;
|
||||
}
|
||||
else
|
||||
{
|
||||
sText = field.Name;
|
||||
}
|
||||
dictionary.Add(sValue, sText);
|
||||
}
|
||||
}
|
||||
return dictionary;
|
||||
}
|
||||
/// <summary>
|
||||
/// 枚举成员转成键值对Json字符串
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <returns></returns>
|
||||
//public static string EnumToDictionaryString(this Type enumType)
|
||||
//{
|
||||
// List<KeyValuePair<int, string>> dictionaryList = EnumToDictionary(enumType).ToList();
|
||||
// var sJson = JsonConvert.SerializeObject(dictionaryList);
|
||||
// return sJson;
|
||||
//}
|
||||
#endregion
|
||||
|
||||
#region 获取枚举的描述
|
||||
/// <summary>
|
||||
/// 获取枚举值对应的描述
|
||||
/// </summary>
|
||||
/// <param name="enumType"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetDescription(this System.Enum enumType)
|
||||
{
|
||||
FieldInfo EnumInfo = enumType.GetType().GetField(enumType.ToString());
|
||||
if (EnumInfo != null)
|
||||
{
|
||||
DescriptionAttribute[] EnumAttributes = (DescriptionAttribute[])EnumInfo.GetCustomAttributes(typeof(DescriptionAttribute), false);
|
||||
if (EnumAttributes.Length > 0)
|
||||
{
|
||||
return EnumAttributes[0].Description;
|
||||
}
|
||||
}
|
||||
return enumType.ToString();
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 根据值获取枚举的描述
|
||||
public static string GetDescriptionByEnum<T>(this object obj)
|
||||
{
|
||||
var tEnum = System.Enum.Parse(typeof(T), obj.ParseToString()) as System.Enum;
|
||||
var description = tEnum.GetDescription();
|
||||
return description;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
public static Exception GetOriginalException(this Exception ex)
|
||||
{
|
||||
if (ex.InnerException == null) return ex;
|
||||
|
||||
return ex.InnerException.GetOriginalException();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static class LinqExtensions
|
||||
{
|
||||
public static Expression Property(this Expression expression, string propertyName)
|
||||
{
|
||||
return Expression.Property(expression, propertyName);
|
||||
}
|
||||
public static Expression AndAlso(this Expression left, Expression right)
|
||||
{
|
||||
return Expression.AndAlso(left, right);
|
||||
}
|
||||
public static Expression Call(this Expression instance, string methodName, params Expression[] arguments)
|
||||
{
|
||||
return Expression.Call(instance, instance.Type.GetMethod(methodName), arguments);
|
||||
}
|
||||
public static Expression GreaterThan(this Expression left, Expression right)
|
||||
{
|
||||
return Expression.GreaterThan(left, right);
|
||||
}
|
||||
public static Expression<T> ToLambda<T>(this Expression body, params ParameterExpression[] parameters)
|
||||
{
|
||||
return Expression.Lambda<T>(body, parameters);
|
||||
}
|
||||
|
||||
public static Expression<Func<T, bool>> True<T>() { return param => true; }
|
||||
|
||||
public static Expression<Func<T, bool>> False<T>() { return param => false; }
|
||||
|
||||
/// <summary>
|
||||
/// 组合And
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
|
||||
{
|
||||
return first.Compose(second, Expression.AndAlso);
|
||||
}
|
||||
/// <summary>
|
||||
/// 组合Or
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
|
||||
{
|
||||
return first.Compose(second, Expression.OrElse);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Combines the first expression with the second using the specified merge function.
|
||||
/// </summary>
|
||||
static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
|
||||
{
|
||||
var map = first.Parameters
|
||||
.Select((f, i) => new { f, s = second.Parameters[i] })
|
||||
.ToDictionary(p => p.s, p => p.f);
|
||||
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
|
||||
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ParameterRebinder
|
||||
/// </summary>
|
||||
private class ParameterRebinder : ExpressionVisitor
|
||||
{
|
||||
/// <summary>
|
||||
/// The ParameterExpression map
|
||||
/// </summary>
|
||||
readonly Dictionary<ParameterExpression, ParameterExpression> map;
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ParameterRebinder"/> class.
|
||||
/// </summary>
|
||||
/// <param name="map">The map.</param>
|
||||
ParameterRebinder(Dictionary<ParameterExpression, ParameterExpression> map)
|
||||
{
|
||||
this.map = map ?? new Dictionary<ParameterExpression, ParameterExpression>();
|
||||
}
|
||||
/// <summary>
|
||||
/// Replaces the parameters.
|
||||
/// </summary>
|
||||
/// <param name="map">The map.</param>
|
||||
/// <param name="exp">The exp.</param>
|
||||
/// <returns>Expression</returns>
|
||||
public static Expression ReplaceParameters(Dictionary<ParameterExpression, ParameterExpression> map, Expression exp)
|
||||
{
|
||||
return new ParameterRebinder(map).Visit(exp);
|
||||
}
|
||||
/// <summary>
|
||||
/// Visits the parameter.
|
||||
/// </summary>
|
||||
/// <param name="p">The p.</param>
|
||||
/// <returns>Expression</returns>
|
||||
protected override Expression VisitParameter(ParameterExpression p)
|
||||
{
|
||||
ParameterExpression replacement;
|
||||
|
||||
if (map.TryGetValue(p, out replacement))
|
||||
{
|
||||
p = replacement;
|
||||
}
|
||||
return base.VisitParameter(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
//using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static partial class Extensions
|
||||
{
|
||||
public static bool IsEmpty(this object value)
|
||||
{
|
||||
if (value != null && !string.IsNullOrEmpty(value.ParseToString()))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
public static bool IsNotEmpty(this object value)
|
||||
{
|
||||
return !IsEmpty(value);
|
||||
}
|
||||
public static bool IsNullOrZero(this object value)
|
||||
{
|
||||
if (value == null || value.ParseToString().Trim() == "0")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
//public static bool IsAjaxRequest(this HttpRequest request)
|
||||
//{
|
||||
// if (request == null)
|
||||
// throw new ArgumentNullException("request");
|
||||
|
||||
// if (request.Headers != null)
|
||||
// return request.Headers["X-Requested-With"] == "XMLHttpRequest";
|
||||
// return false;
|
||||
//}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Extensions
|
||||
{
|
||||
public static class StringExtension
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// SQL条件拼接
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <param name="condition"></param>
|
||||
/// <returns></returns>
|
||||
public static string If(this string str, bool condition)
|
||||
{
|
||||
return condition ? str : string.Empty;
|
||||
}
|
||||
/// <summary>
|
||||
/// 判断是否为空
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IfNotEmpty(this string str)
|
||||
{
|
||||
return !string.IsNullOrEmpty(str);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注意:如果替换的旧值中有特殊符号,替换将会失败,解决办法 例如特殊符号是“(”: 要在调用本方法前加oldValue=oldValue.Replace("(","//(");
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="oldValue"></param>
|
||||
/// <param name="newValue"></param>
|
||||
/// <returns></returns>
|
||||
public static string ReplaceFirst(this string input, string oldValue, string newValue)
|
||||
{
|
||||
Regex regEx = new Regex(oldValue, RegexOptions.Multiline);
|
||||
return regEx.Replace(input, newValue == null ? "" : newValue, 1);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 骆驼峰转下划线
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <returns></returns>
|
||||
public static string ToSmallCamelCase(string name)
|
||||
{
|
||||
var stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append(name.Substring(0, 1).ToLower());
|
||||
|
||||
for (var i = 0; i < name.Length; i++)
|
||||
{
|
||||
if (i == 0)
|
||||
{
|
||||
stringBuilder.Append(name.Substring(0, 1).ToLower());
|
||||
}
|
||||
else
|
||||
{
|
||||
if (name[i] >= 'A' && name[i] <= 'Z')
|
||||
{
|
||||
stringBuilder.Append($"_{name.Substring(i, 1).ToLower()}");
|
||||
}
|
||||
else
|
||||
{
|
||||
stringBuilder.Append(name[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return stringBuilder.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 下划线命名转驼峰命名
|
||||
/// </summary>
|
||||
/// <param name="underscore"></param>
|
||||
/// <returns></returns>
|
||||
public static string UnderScoreToCamelCase(this string underscore)
|
||||
{
|
||||
string[] ss = underscore.Split("_");
|
||||
if (ss.Length == 1)
|
||||
{
|
||||
return underscore;
|
||||
}
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.Append(ss[0]);
|
||||
for (int i = 1; i < ss.Length; i++)
|
||||
{
|
||||
sb.Append(ss[i].FirstUpperCase());
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 首字母转大写
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string FirstUpperCase(this string str)
|
||||
{
|
||||
return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToUpper() + str[1..];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 首字母转小写
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static string FirstLowerCase(this string str)
|
||||
{
|
||||
return string.IsNullOrEmpty(str) ? str : str.Substring(0, 1).ToLower() + str[1..];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 截取指定字符串中间内容
|
||||
/// </summary>
|
||||
/// <param name="sourse"></param>
|
||||
/// <param name="startstr"></param>
|
||||
/// <param name="endstr"></param>
|
||||
/// <returns></returns>
|
||||
public static string SubstringBetween(this string sourse, string startstr, string endstr)
|
||||
{
|
||||
string result = string.Empty;
|
||||
int startindex, endindex;
|
||||
try
|
||||
{
|
||||
startindex = sourse.IndexOf(startstr);
|
||||
if (startindex == -1)
|
||||
return result;
|
||||
string tmpstr = sourse.Substring(startindex + startstr.Length);
|
||||
endindex = tmpstr.IndexOf(endstr);
|
||||
if (endindex == -1)
|
||||
return result;
|
||||
result = tmpstr.Remove(endindex);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("MidStrEx Err:" + ex.Message);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为Pascal风格-每一个单词的首字母大写
|
||||
/// </summary>
|
||||
/// <param name="fieldName">字段名</param>
|
||||
/// <param name="fieldDelimiter">分隔符</param>
|
||||
/// <returns></returns>
|
||||
public static string ConvertToPascal(this string fieldName, string fieldDelimiter)
|
||||
{
|
||||
string result = string.Empty;
|
||||
if (fieldName.Contains(fieldDelimiter))
|
||||
{
|
||||
//全部小写
|
||||
string[] array = fieldName.ToLower().Split(fieldDelimiter.ToCharArray());
|
||||
foreach (var t in array)
|
||||
{
|
||||
//首字母大写
|
||||
result += t.Substring(0, 1).ToUpper() + t[1..];
|
||||
}
|
||||
}
|
||||
else if (string.IsNullOrWhiteSpace(fieldName))
|
||||
{
|
||||
result = fieldName;
|
||||
}
|
||||
else if (fieldName.Length == 1)
|
||||
{
|
||||
result = fieldName.ToUpper();
|
||||
}
|
||||
else if (fieldName.Length == fieldName.CountUpper())
|
||||
{
|
||||
result = fieldName.Substring(0, 1).ToUpper() + fieldName[1..].ToLower();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = fieldName.Substring(0, 1).ToUpper() + fieldName[1..];
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 大写字母个数
|
||||
/// </summary>
|
||||
/// <param name="str"></param>
|
||||
/// <returns></returns>
|
||||
public static int CountUpper(this string str)
|
||||
{
|
||||
int count1 = 0;
|
||||
char[] chars = str.ToCharArray();
|
||||
foreach (char num in chars)
|
||||
{
|
||||
if (num >= 'A' && num <= 'Z')
|
||||
{
|
||||
count1++;
|
||||
}
|
||||
//else if (num >= 'a' && num <= 'z')
|
||||
//{
|
||||
// count2++;
|
||||
//}
|
||||
}
|
||||
return count1;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 转换为Camel风格-第一个单词小写,其后每个单词首字母大写
|
||||
/// </summary>
|
||||
/// <param name="fieldName">字段名</param>
|
||||
/// <param name="fieldDelimiter">分隔符</param>
|
||||
/// <returns></returns>
|
||||
public static string ConvertToCamel(this string fieldName, string fieldDelimiter)
|
||||
{
|
||||
//先Pascal
|
||||
string result = fieldName.ConvertToPascal(fieldDelimiter);
|
||||
//然后首字母小写
|
||||
if (result.Length == 1)
|
||||
{
|
||||
result = result.ToLower();
|
||||
}
|
||||
else
|
||||
{
|
||||
result = result.Substring(0, 1).ToLower() + result[1..];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using Newtonsoft.Json;
|
||||
using Yi.Framework.Infrastructure.Extensions;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Helper
|
||||
{
|
||||
public class ComputerHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 内存使用情况
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static MemoryMetrics GetComputerInfo()
|
||||
{
|
||||
try
|
||||
{
|
||||
MemoryMetricsClient client = new();
|
||||
MemoryMetrics memoryMetrics = IsUnix() ? client.GetUnixMetrics() : client.GetWindowsMetrics();
|
||||
|
||||
memoryMetrics.FreeRam = Math.Round(memoryMetrics.Free / 1024, 2) + "GB";
|
||||
memoryMetrics.UsedRam = Math.Round(memoryMetrics.Used / 1024, 2) + "GB";
|
||||
memoryMetrics.TotalRAM = Math.Round(memoryMetrics.Total / 1024, 2) + "GB";
|
||||
memoryMetrics.RAMRate = Math.Ceiling(100 * memoryMetrics.Used / memoryMetrics.Total).ToString() + "%";
|
||||
memoryMetrics.CPURate = Math.Ceiling(GetCPURate().ParseToDouble()) + "%";
|
||||
return memoryMetrics;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("获取内存使用出错,msg=" + ex.Message + "," + ex.StackTrace);
|
||||
}
|
||||
return new MemoryMetrics();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取内存大小
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<DiskInfo> GetDiskInfos()
|
||||
{
|
||||
List<DiskInfo> diskInfos = new();
|
||||
|
||||
if (IsUnix())
|
||||
{
|
||||
try
|
||||
{
|
||||
string output = ShellHelper.Bash("df -m / | awk '{print $2,$3,$4,$5,$6}'");
|
||||
var arr = output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
||||
if (arr.Length == 0) return diskInfos;
|
||||
|
||||
var rootDisk = arr[1].Split(' ', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
if (rootDisk == null || rootDisk.Length == 0)
|
||||
{
|
||||
return diskInfos;
|
||||
}
|
||||
DiskInfo diskInfo = new()
|
||||
{
|
||||
DiskName = "/",
|
||||
TotalSize = long.Parse(rootDisk[0]) / 1024,
|
||||
Used = long.Parse(rootDisk[1]) / 1024,
|
||||
AvailableFreeSpace = long.Parse(rootDisk[2]) / 1024,
|
||||
AvailablePercent = decimal.Parse(rootDisk[3].Replace("%", ""))
|
||||
};
|
||||
diskInfos.Add(diskInfo);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("获取磁盘信息出错了" + ex.Message);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
var driv = DriveInfo.GetDrives();
|
||||
foreach (var item in driv)
|
||||
{
|
||||
try
|
||||
{
|
||||
var obj = new DiskInfo()
|
||||
{
|
||||
DiskName = item.Name,
|
||||
TypeName = item.DriveType.ToString(),
|
||||
TotalSize = item.TotalSize / 1024 / 1024 / 1024,
|
||||
AvailableFreeSpace = item.AvailableFreeSpace / 1024 / 1024 / 1024,
|
||||
};
|
||||
obj.Used = obj.TotalSize - obj.AvailableFreeSpace;
|
||||
obj.AvailablePercent = decimal.Ceiling(obj.Used / (decimal)obj.TotalSize * 100);
|
||||
diskInfos.Add(obj);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("获取磁盘信息出错了" + ex.Message);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return diskInfos;
|
||||
}
|
||||
|
||||
public static bool IsUnix()
|
||||
{
|
||||
var isUnix = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
|
||||
return isUnix;
|
||||
}
|
||||
|
||||
public static string GetCPURate()
|
||||
{
|
||||
string cpuRate;
|
||||
if (IsUnix())
|
||||
{
|
||||
string output = ShellHelper.Bash("top -b -n1 | grep \"Cpu(s)\" | awk '{print $2 + $4}'");
|
||||
cpuRate = output.Trim();
|
||||
}
|
||||
else
|
||||
{
|
||||
string output = ShellHelper.Cmd("wmic", "cpu get LoadPercentage");
|
||||
cpuRate = output.Replace("LoadPercentage", string.Empty).Trim();
|
||||
}
|
||||
return cpuRate;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取系统运行时间
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetRunTime()
|
||||
{
|
||||
string runTime = string.Empty;
|
||||
try
|
||||
{
|
||||
if (IsUnix())
|
||||
{
|
||||
string output = ShellHelper.Bash("uptime -s").Trim();
|
||||
runTime = DateTimeHelper.FormatTime((DateTime.Now - output.ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
|
||||
}
|
||||
else
|
||||
{
|
||||
string output = ShellHelper.Cmd("wmic", "OS get LastBootUpTime/Value");
|
||||
string[] outputArr = output.Split('=', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
if (outputArr.Length == 2)
|
||||
{
|
||||
runTime = DateTimeHelper.FormatTime((DateTime.Now - outputArr[1].Split('.')[0].ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine("获取runTime出错" + ex.Message);
|
||||
}
|
||||
return runTime;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内存信息
|
||||
/// </summary>
|
||||
public class MemoryMetrics
|
||||
{
|
||||
[JsonIgnore]
|
||||
public double Total { get; set; }
|
||||
[JsonIgnore]
|
||||
public double Used { get; set; }
|
||||
[JsonIgnore]
|
||||
public double Free { get; set; }
|
||||
|
||||
public string UsedRam { get; set; }
|
||||
/// <summary>
|
||||
/// CPU使用率%
|
||||
/// </summary>
|
||||
public string CPURate { get; set; }
|
||||
/// <summary>
|
||||
/// 总内存 GB
|
||||
/// </summary>
|
||||
public string TotalRAM { get; set; }
|
||||
/// <summary>
|
||||
/// 内存使用率 %
|
||||
/// </summary>
|
||||
public string RAMRate { get; set; }
|
||||
/// <summary>
|
||||
/// 空闲内存
|
||||
/// </summary>
|
||||
public string FreeRam { get; set; }
|
||||
}
|
||||
|
||||
public class DiskInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 磁盘名
|
||||
/// </summary>
|
||||
public string DiskName { get; set; }
|
||||
public string TypeName { get; set; }
|
||||
public long TotalFree { get; set; }
|
||||
public long TotalSize { get; set; }
|
||||
/// <summary>
|
||||
/// 已使用
|
||||
/// </summary>
|
||||
public long Used { get; set; }
|
||||
/// <summary>
|
||||
/// 可使用
|
||||
/// </summary>
|
||||
public long AvailableFreeSpace { get; set; }
|
||||
public decimal AvailablePercent { get; set; }
|
||||
}
|
||||
|
||||
public class MemoryMetricsClient
|
||||
{
|
||||
#region 获取内存信息
|
||||
|
||||
/// <summary>
|
||||
/// windows系统获取内存信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public MemoryMetrics GetWindowsMetrics()
|
||||
{
|
||||
string output = ShellHelper.Cmd("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value");
|
||||
var metrics = new MemoryMetrics();
|
||||
var lines = output.Trim().Split('\n', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (lines.Length <= 0) return metrics;
|
||||
|
||||
var freeMemoryParts = lines[0].Split('=', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
var totalMemoryParts = lines[1].Split('=', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
metrics.Total = Math.Round(double.Parse(totalMemoryParts[1]) / 1024, 0);
|
||||
metrics.Free = Math.Round(double.Parse(freeMemoryParts[1]) / 1024, 0);//m
|
||||
metrics.Used = metrics.Total - metrics.Free;
|
||||
|
||||
return metrics;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unix系统获取
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public MemoryMetrics GetUnixMetrics()
|
||||
{
|
||||
string output = ShellHelper.Bash("free -m | awk '{print $2,$3,$4,$5,$6}'");
|
||||
var metrics = new MemoryMetrics();
|
||||
var lines = output.Split('\n', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
|
||||
if (lines.Length <= 0) return metrics;
|
||||
|
||||
if (lines != null && lines.Length > 0)
|
||||
{
|
||||
var memory = lines[1].Split(' ', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||
if (memory.Length >= 3)
|
||||
{
|
||||
metrics.Total = double.Parse(memory[0]);
|
||||
metrics.Used = double.Parse(memory[1]);
|
||||
metrics.Free = double.Parse(memory[2]);//m
|
||||
}
|
||||
}
|
||||
return metrics;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Helper
|
||||
{
|
||||
public class DateTimeHelper
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="dateTime"></param>
|
||||
/// <returns></returns>
|
||||
public static DateTime GetBeginTime(DateTime? dateTime, int days = 0)
|
||||
{
|
||||
if (dateTime == DateTime.MinValue || dateTime == null)
|
||||
{
|
||||
return DateTime.Now.AddDays(days);
|
||||
}
|
||||
return dateTime ?? DateTime.Now;
|
||||
}
|
||||
#region 时间戳转换
|
||||
|
||||
/// <summary>
|
||||
/// 时间戳转本地时间-时间戳精确到秒
|
||||
/// </summary>
|
||||
public static DateTime ToLocalTimeDateBySeconds(long unix)
|
||||
{
|
||||
var dto = DateTimeOffset.FromUnixTimeSeconds(unix);
|
||||
return dto.ToLocalTime().DateTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 时间转时间戳Unix-时间戳精确到秒
|
||||
/// </summary>
|
||||
public static long ToUnixTimestampBySeconds(DateTime dt)
|
||||
{
|
||||
DateTimeOffset dto = new DateTimeOffset(dt);
|
||||
return dto.ToUnixTimeSeconds();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 时间戳转本地时间-时间戳精确到毫秒
|
||||
/// </summary>
|
||||
public static DateTime ToLocalTimeDateByMilliseconds(long unix)
|
||||
{
|
||||
var dto = DateTimeOffset.FromUnixTimeMilliseconds(unix);
|
||||
return dto.ToLocalTime().DateTime;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 时间转时间戳Unix-时间戳精确到毫秒
|
||||
/// </summary>
|
||||
public static long ToUnixTimestampByMilliseconds(DateTime dt)
|
||||
{
|
||||
DateTimeOffset dto = new DateTimeOffset(dt);
|
||||
return dto.ToUnixTimeMilliseconds();
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 毫秒转天时分秒
|
||||
/// <summary>
|
||||
/// 毫秒转天时分秒
|
||||
/// </summary>
|
||||
/// <param name="ms"></param>
|
||||
/// <returns></returns>
|
||||
public static string FormatTime(long ms)
|
||||
{
|
||||
int ss = 1000;
|
||||
int mi = ss * 60;
|
||||
int hh = mi * 60;
|
||||
int dd = hh * 24;
|
||||
|
||||
long day = ms / dd;
|
||||
long hour = (ms - day * dd) / hh;
|
||||
long minute = (ms - day * dd - hour * hh) / mi;
|
||||
long second = (ms - day * dd - hour * hh - minute * mi) / ss;
|
||||
long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;
|
||||
|
||||
string sDay = day < 10 ? "0" + day : "" + day; //天
|
||||
string sHour = hour < 10 ? "0" + hour : "" + hour;//小时
|
||||
string sMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
|
||||
string sSecond = second < 10 ? "0" + second : "" + second;//秒
|
||||
string sMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
|
||||
sMilliSecond = milliSecond < 100 ? "0" + sMilliSecond : "" + sMilliSecond;
|
||||
|
||||
return string.Format("{0} 天 {1} 小时 {2} 分 {3} 秒", sDay, sHour, sMinute, sSecond);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取unix时间戳
|
||||
/// <summary>
|
||||
/// 获取unix时间戳
|
||||
/// </summary>
|
||||
/// <param name="dt"></param>
|
||||
/// <returns></returns>
|
||||
public static long GetUnixTimeStamp(DateTime dt)
|
||||
{
|
||||
long unixTime = ((DateTimeOffset)dt).ToUnixTimeMilliseconds();
|
||||
return unixTime;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取日期天的最小时间
|
||||
public static DateTime GetDayMinDate(DateTime dt)
|
||||
{
|
||||
DateTime min = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0);
|
||||
return min;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取日期天的最大时间
|
||||
public static DateTime GetDayMaxDate(DateTime dt)
|
||||
{
|
||||
DateTime max = new DateTime(dt.Year, dt.Month, dt.Day, 23, 59, 59);
|
||||
return max;
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region 获取日期天的最大时间
|
||||
public static string FormatDateTime(DateTime? dt)
|
||||
{
|
||||
if (dt != null)
|
||||
{
|
||||
if (dt.Value.Year == DateTime.Now.Year)
|
||||
{
|
||||
return dt.Value.ToString("MM-dd HH:mm");
|
||||
}
|
||||
else
|
||||
{
|
||||
return dt.Value.ToString("yyyy-MM-dd HH:mm");
|
||||
}
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Text;
|
||||
|
||||
namespace Yi.Framework.Infrastructure.Helper
|
||||
{
|
||||
public class ShellHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// linux 系统命令
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
/// <returns></returns>
|
||||
public static string Bash(string command)
|
||||
{
|
||||
var escapedArgs = command.Replace("\"", "\\\"");
|
||||
var process = new Process()
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = "/bin/bash",
|
||||
Arguments = $"-c \"{escapedArgs}\"",
|
||||
RedirectStandardOutput = true,
|
||||
UseShellExecute = false,
|
||||
CreateNoWindow = true,
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
string result = process.StandardOutput.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
process.Dispose();
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// windows系统命令
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="args"></param>
|
||||
/// <returns></returns>
|
||||
public static string Cmd(string fileName, string args)
|
||||
{
|
||||
string output = string.Empty;
|
||||
|
||||
var info = new ProcessStartInfo();
|
||||
info.FileName = fileName;
|
||||
info.Arguments = args;
|
||||
info.RedirectStandardOutput = true;
|
||||
|
||||
using (var process = Process.Start(info))
|
||||
{
|
||||
output = process.StandardOutput.ReadToEnd();
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user