初始化项目
This commit is contained in:
17
Services/Tool/System/ComputerHelp.cs
Normal file
17
Services/Tool/System/ComputerHelp.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Services.Tool
|
||||
{
|
||||
public class ComputerHelp
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
159
Services/Tool/System/ConfigHelper.cs
Normal file
159
Services/Tool/System/ConfigHelper.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
using Services.Extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Web.Configuration;
|
||||
|
||||
namespace Services.Tool
|
||||
{
|
||||
/// <summary>
|
||||
/// web.config操作类
|
||||
/// Copyright (C) Maticsoft
|
||||
/// </summary>
|
||||
public sealed class ConfigHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// 得到AppSettings中的配置字符串信息
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static string GetConfigString(string key)
|
||||
{
|
||||
string CacheKey = "AppSettings-" + key;
|
||||
object objModel = CacheExtensions.GetCache<object>(CacheKey);
|
||||
if (objModel == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
objModel = ConfigurationManager.AppSettings[key];
|
||||
if (objModel != null)
|
||||
{
|
||||
CacheExtensions.SetCache(CacheKey, objModel, Services.Enums.CacheTimeType.ByHours, 3);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{ }
|
||||
}
|
||||
return objModel.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 得到AppSettings中的配置Bool信息
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static bool GetConfigBool(string key)
|
||||
{
|
||||
bool result = false;
|
||||
string cfgVal = GetConfigString(key);
|
||||
if (null != cfgVal && string.Empty != cfgVal)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = bool.Parse(cfgVal);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
// Ignore format exceptions.
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 得到AppSettings中的配置Decimal信息
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static decimal GetConfigDecimal(string key)
|
||||
{
|
||||
decimal result = 0;
|
||||
string cfgVal = GetConfigString(key);
|
||||
if (null != cfgVal && string.Empty != cfgVal)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = decimal.Parse(cfgVal);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
// Ignore format exceptions.
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
/// <summary>
|
||||
/// 得到AppSettings中的配置int信息
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static int GetConfigInt(string key)
|
||||
{
|
||||
int result = 0;
|
||||
string cfgVal = GetConfigString(key);
|
||||
if (null != cfgVal && string.Empty != cfgVal)
|
||||
{
|
||||
try
|
||||
{
|
||||
result = int.Parse(cfgVal);
|
||||
}
|
||||
catch (FormatException)
|
||||
{
|
||||
// Ignore format exceptions.
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 修改AppSettings中的配置信息
|
||||
/// </summary>
|
||||
/// <param name="key"></param>
|
||||
/// <returns></returns>
|
||||
public static void UpdatetConfig(string key, string value)
|
||||
{
|
||||
Configuration objConfig = WebConfigurationManager.OpenWebConfiguration("~");
|
||||
AppSettingsSection objAppSettings = (AppSettingsSection)objConfig.GetSection("appSettings");
|
||||
if (objAppSettings != null)
|
||||
{
|
||||
objAppSettings.Settings[key].Value = value;
|
||||
objConfig.Save();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 加载配置文件 app升级配置文件
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static Dictionary<String, String> loadCfg()
|
||||
{
|
||||
string cfgPath = Path.GetDirectoryName(AppDomain.CurrentDomain.SetupInformation.ApplicationBase)
|
||||
+ Path.DirectorySeparatorChar + "appset" + Path.DirectorySeparatorChar + "appconfig.properties";
|
||||
Dictionary<String, String> cfg = new Dictionary<string, string>();
|
||||
using (StreamReader sr = new StreamReader(cfgPath))
|
||||
{
|
||||
while (sr.Peek() >= 0)
|
||||
{
|
||||
string line = sr.ReadLine();
|
||||
if (line.StartsWith("#"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
int startInd = line.IndexOf("=");
|
||||
string key = line.Substring(0, startInd);
|
||||
string val = line.Substring(startInd + 1, line.Length - (startInd + 1));
|
||||
if (!cfg.ContainsKey(key) && !string.IsNullOrEmpty(val))
|
||||
{
|
||||
cfg.Add(key, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cfg;
|
||||
}
|
||||
}
|
||||
}
|
||||
43
Services/Tool/System/EnumHelper.cs
Normal file
43
Services/Tool/System/EnumHelper.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Services.Tool
|
||||
{
|
||||
/// <summary>
|
||||
/// 枚举帮助类
|
||||
/// </summary>
|
||||
public static class EnumHelper
|
||||
{
|
||||
|
||||
///// <summary>
|
||||
///// 获取枚举的Short类型
|
||||
///// </summary>
|
||||
///// <param name="value"></param>
|
||||
///// <returns></returns>
|
||||
//public static short GetShortValue(Enum value)
|
||||
//{
|
||||
// return short.Parse(((int)Enum.Parse(value.GetType(), value.ToString())).ToString());
|
||||
//}
|
||||
|
||||
///// <summary>
|
||||
///// 得到枚举中文备注
|
||||
///// </summary>
|
||||
///// <param name="enumValue"></param>
|
||||
///// <returns></returns>
|
||||
//public static string GetEnumDesc(Enum enumValue)
|
||||
//{
|
||||
// string value = enumValue.ToString();
|
||||
// FieldInfo field = enumValue.GetType().GetField(value);
|
||||
// object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描述属性
|
||||
// if (objs.Length == 0) //当描述属性没有时,直接返回名称
|
||||
// return value;
|
||||
// DescriptionAttribute descriptionAttribute = (DescriptionAttribute)objs[0];
|
||||
// return descriptionAttribute.Description;
|
||||
//}
|
||||
}
|
||||
}
|
||||
86
Services/Tool/System/IPHelper.cs
Normal file
86
Services/Tool/System/IPHelper.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading.Tasks;
|
||||
using System.Web;
|
||||
|
||||
namespace Services.Tool
|
||||
{
|
||||
public class IPHelper
|
||||
{
|
||||
public static string GetIP()
|
||||
{
|
||||
if (HttpContext.Current == null)
|
||||
{
|
||||
throw new Exception("HttpContext.Current为null");
|
||||
}
|
||||
string ip = string.Empty;
|
||||
if (!string.IsNullOrEmpty(HttpContext.Current.Request.ServerVariables["HTTP_VIA"]))
|
||||
ip = Convert.ToString(HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]);
|
||||
if (string.IsNullOrEmpty(ip))
|
||||
ip = Convert.ToString(HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"]);
|
||||
return ip;
|
||||
}
|
||||
|
||||
/// <summary>获取客户端的IP,可以取到代理后的IP
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static string GetClientIp()
|
||||
{
|
||||
string result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
|
||||
if (!string.IsNullOrEmpty(result))
|
||||
{
|
||||
//可能有代理
|
||||
if (result.IndexOf(".", StringComparison.Ordinal) == -1)
|
||||
result = null;
|
||||
else
|
||||
{
|
||||
if (result.IndexOf(",", StringComparison.Ordinal) != -1)
|
||||
{
|
||||
//有“,”,估计多个代理。取第一个不是内网的IP。
|
||||
result = result.Replace(" ", "").Replace("'", "");
|
||||
string[] temparyip = result.Split(",;".ToCharArray());
|
||||
foreach (string t in temparyip)
|
||||
{
|
||||
if (IsIpAddress(t)
|
||||
&& t.Substring(0, 3) != "10."
|
||||
&& t.Substring(0, 7) != "192.168"
|
||||
&& t.Substring(0, 7) != "172.16.")
|
||||
{
|
||||
return t; //找到不是内网的地址
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (IsIpAddress(result)) //代理即是IP格式
|
||||
return result;
|
||||
else
|
||||
result = null; //代理中的内容 非IP,取IP
|
||||
}
|
||||
}
|
||||
if (string.IsNullOrEmpty(result))
|
||||
result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
|
||||
if (string.IsNullOrEmpty(result))
|
||||
result = HttpContext.Current.Request.UserHostAddress;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>判断是否是IP地址格式 0.0.0.0
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="str">待判断的IP地址</param>
|
||||
/// <returns>true or false</returns>
|
||||
public static bool IsIpAddress(string str)
|
||||
{
|
||||
if (string.IsNullOrEmpty(str) || str.Length < 7 || str.Length > 15) return false;
|
||||
|
||||
const string regformat = @"^d{1,3}[.]d{1,3}[.]d{1,3}[.]d{1,3}$";
|
||||
|
||||
Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);
|
||||
return regex.IsMatch(str);
|
||||
}
|
||||
}
|
||||
}
|
||||
1194
Services/Tool/System/StringHelp.cs
Normal file
1194
Services/Tool/System/StringHelp.cs
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user