在项目开发中公共帮助类是必不可少的,这里记录一些自己摘录或自己编写的帮助类。

64位编码与解码:

 #region URL的64位编码
/// <summary>
/// URL的64位编码
/// </summary>
/// <param name="sourthUrl"></param>
/// <returns></returns>
public static string Base64Encrypt(string sourthUrl)
{
string eurl = HttpUtility.UrlEncode(sourthUrl);
eurl = Convert.ToBase64String(Encoding.Default.GetBytes(eurl));
return eurl;
}
#endregion #region URL的64位解码
/// <summary>
/// URL的64位解码
/// </summary>
/// <param name="eStr"></param>
/// <returns></returns>
public static string Base64Decrypt(string eStr)
{
if (!IsBase64(eStr))
{
return eStr;
}
byte[] buffer = Convert.FromBase64String(eStr);
string sourthUrl = Encoding.Default.GetString(buffer);
sourthUrl = HttpUtility.UrlDecode(sourthUrl);
return sourthUrl;
}
#endregion #region 检查一个字符串是否是Base64字符串
/// <summary>
/// 检查一个字符串是否是Base64字符串
/// </summary>
/// <param name="eStr"></param>
/// <returns></returns>
public static bool IsBase64(string eStr)
{
if ((eStr.Length % ) != )
{
return false;
}
if (!Regex.IsMatch(eStr, "^[A-Z0-9/+=]*$", RegexOptions.IgnoreCase))
{
return false;
}
return true;
}
#endregion

字符串过滤:

 #region 检测是否有Sql危险字符
/// <summary>
/// 检测是否有Sql危险字符
/// </summary>
/// <param name="str">要判断字符串</param>
/// <returns>判断结果</returns>
public static bool IsSafeSqlString(string str)
{
return !Regex.IsMatch(str, @"[-|;|,|\/|\(|\)|\[|\]|\}|\{|%|@|\*|!|\']");
} /// <summary>
/// 检查危险字符
/// </summary>
/// <param name="Input"></param>
/// <returns></returns>
public static string Filter(string sInput)
{
if (sInput == null || sInput == "")
return null;
string sInput1 = sInput.ToLower();
string output = sInput;
string pattern = @"*|and|exec|insert|select|delete|update|count|master|truncate|declare|char(|mid(|chr(|'";
if (Regex.Match(sInput1, Regex.Escape(pattern), RegexOptions.Compiled | RegexOptions.IgnoreCase).Success)
{
throw new Exception("字符串中含有非法字符!");
}
else
{
output = output.Replace("'", "''");
}
return output;
} /// <summary>
/// 检查过滤设定的危险字符
/// </summary>
/// <param name="InText">要过滤的字符串 </param>
/// <returns>如果参数存在不安全字符,则返回true </returns>
public static bool SqlFilter(string word, string InText)
{
if (InText == null)
return false;
foreach (string i in word.Split('|'))
{
if ((InText.ToLower().IndexOf(i + " ") > -) || (InText.ToLower().IndexOf(" " + i) > -))
{
return true;
}
}
return false;
}
#endregion #region 过滤特殊字符
/// <summary>
/// 过滤特殊字符
/// </summary>
/// <param name="Input"></param>
/// <returns></returns>
public static string Htmls(string Input)
{
if (Input != string.Empty && Input != null)
{
string ihtml = Input.ToLower();
ihtml = ihtml.Replace("<script", "&lt;script");
ihtml = ihtml.Replace("script>", "script&gt;");
ihtml = ihtml.Replace("<%", "&lt;%");
ihtml = ihtml.Replace("%>", "%&gt;");
ihtml = ihtml.Replace("<$", "&lt;$");
ihtml = ihtml.Replace("$>", "$&gt;");
return ihtml;
}
else
{
return string.Empty;
}
}
#endregion

字符串与List相互转化:

        #region 把字符串按照分隔符转换成 List
/// <summary>
/// 把字符串按照分隔符转换成 List
/// </summary>
/// <param name="str">源字符串</param>
/// <param name="speater">分隔符</param>
/// <param name="toLower">是否转换为小写</param>
/// <returns></returns>
public static List<string> GetStrArray(string str, char speater, bool toLower)
{
List<string> list = new List<string>();
string[] ss = str.Split(speater);
foreach (string s in ss)
{
if (!string.IsNullOrEmpty(s) && s != speater.ToString())
{
string strVal = s;
if (toLower)
{
strVal = s.ToLower();
}
list.Add(strVal);
}
}
return list;
}
#endregion #region 把 List<string> 按照分隔符组装成 string
/// <summary>
/// 把 List<string> 按照分隔符组装成 string
/// </summary>
/// <param name="list"></param>
/// <param name="speater">分隔符</param>
/// <returns></returns>
public static string GetArrayStr(List<string> list, string speater)
{
StringBuilder sb = new StringBuilder();
for (int i = ; i < list.Count; i++)
{
if (i == list.Count - )
{
sb.Append(list[i]);
}
else
{
sb.Append(list[i]);
sb.Append(speater);
}
}
return sb.ToString();
}
#endregion #region 把 List<int> 按照分隔符组装成 string
/// <summary>
/// 把 List<int> 按照分隔符组装成 string
/// </summary>
/// <param name="list"></param>
/// <param name="speater">分隔符</param>
/// <returns></returns>
public static string GetArrayStr(List<int> list, string speater)
{
StringBuilder sb = new StringBuilder();
for (int i = ; i < list.Count; i++)
{
if (i == list.Count - )
{
sb.Append(list[i].ToString());
}
else
{
sb.Append(list[i]);
sb.Append(",");
}
}
return sb.ToString();
}
#endregion

HTML转成Text文本:

/// <summary>
/// HTML转行成TEXT
/// </summary>
/// <param name="strHtml"></param>
/// <returns></returns>
public static string HtmlToTxt(string strHtml)
{
string[] aryReg ={
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\[""'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>",
@"([\r\n])[\s]+",
@"&(quot|#34);",
@"&(amp|#38);",
@"&(lt|#60);",
@"&(gt|#62);",
@"&(nbsp|#160);",
@"&(iexcl|#161);",
@"&(cent|#162);",
@"&(pound|#163);",
@"&(copy|#169);",
@"&#(\d+);",
@"-->",
@"<!--.*\n"
}; string newReg = aryReg[];
string strOutput = strHtml;
for (int i = ; i < aryReg.Length; i++)
{
Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, string.Empty);
} strOutput.Replace("<", "");
strOutput.Replace(">", "");
strOutput.Replace("\r\n", ""); return strOutput;
}

DataTable转List<T>:

 /// <summary>
/// DataTable转换成List<T>
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <param name="IsDataField"></param>
/// <returns></returns>
public static List<T> DataTableToList<T>(DataTable dt, bool IsDataField) where T : new()
{
List<T> ts = new List<T>();
Type type = typeof(T);
string tempName = string.Empty;
foreach (DataRow dr in dt.Rows)
{
T myt = new T();
PropertyInfo[] propertys = myt.GetType().GetProperties();
PropertyInfo[] array = propertys;
int i = ;
DataFieldAttribute attribute = null;
PropertyAttribute proAttribute = null;
while (i < array.Length)
{
PropertyInfo pi = array[i];
if (IsDataField)
{
object[] infos = null;
object[] pros = null;
infos = pi.GetCustomAttributes(typeof(DataFieldAttribute), false);
pros = pi.GetCustomAttributes(typeof(PropertyAttribute), false);
if (infos.Length > )
{
attribute = (DataFieldAttribute)(infos[]);
if (pros.Length > )
{
proAttribute = (PropertyAttribute)(pros[]);
if (proAttribute.columnKeyType != ColumnKeyType.Extend)
{
tempName = attribute.FieldName;
}
}
else
tempName = attribute.FieldName;
}
}
else
tempName = pi.Name;
if (dt.Columns.Contains(tempName))
{
if (pi.CanWrite)
{
object value = dr[tempName];
//if (value.GetType().Equals(typeof(DateTime)))
// value = System.Convert.ToString(value);
if (value != DBNull.Value)
pi.SetValue(myt, value, null);
}
}
i += ;
continue;
}
ts.Add(myt);
}
return ts;
}

DataTable 转 Object:

/// <summary>
/// DataTable 转 Object
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dt"></param>
/// <returns></returns>
public static T DataTableToObject<T>(DataTable dt, bool IsDataField) where T : new()
{
Type type = typeof(T);
string tempName = string.Empty;
T myt = new T();
PropertyInfo[] propertys = myt.GetType().GetProperties();
PropertyInfo[] array = propertys;
int i = ;
DataFieldAttribute attribute = null;
PropertyAttribute proAttribute = null;
while (i < array.Length)
{
PropertyInfo pi = array[i];
if (IsDataField)
{
object[] infos = null;
object[] pros = null;
infos = pi.GetCustomAttributes(typeof(DataFieldAttribute), false);
pros = pi.GetCustomAttributes(typeof(PropertyAttribute), false);
if (infos.Length > )
{
attribute = (DataFieldAttribute)(infos[]);
if (pros.Length>)
{
proAttribute = (PropertyAttribute)(pros[]);
if (proAttribute.columnKeyType != ColumnKeyType.Extend)
{
tempName = attribute.FieldName;
}
}else
tempName = attribute.FieldName;
}
}
else
tempName = pi.Name; if (dt.Columns.Contains(tempName))
{
if (pi.CanWrite)
{
object value = dt.Rows[][tempName];
//if (value.GetType().Equals(typeof(DateTime)))
// value = Convert.ToString(value);
if (value != DBNull.Value)
pi.SetValue(myt, value, null);
}
}
i += ;
continue;
}
return myt;
}

CacheHelper:

/// <summary>
/// 获取数据缓存
/// </summary>
/// <param name="CacheKey">键</param>
public static object GetCache(string CacheKey)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
return objCache[CacheKey];
} /// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject);
} /// <summary>
/// 设置数据缓存
/// </summary>
public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)
{
System.Web.Caching.Cache objCache = HttpRuntime.Cache;
objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);
} /// <summary>
/// 移除指定数据缓存
/// </summary>
public static void RemoveAllCache(string CacheKey)
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
_cache.Remove(CacheKey);
} /// <summary>
/// 移除全部缓存
/// </summary>
public static void RemoveAllCache()
{
System.Web.Caching.Cache _cache = HttpRuntime.Cache;
IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
_cache.Remove(CacheEnum.Key.ToString());
}
}
 

最新文章

  1. nginx缓存引发的问题
  2. XAF 14.1 DC 实现自定审计日志信息
  3. HTML5商城开发四 多图或多商品的水平滚动展示
  4. nginx rewirte
  5. [Python] Python 之 __new__() 方法与实例化
  6. 【Java】Eclipse导出jar包与javadoc
  7. C# Regex类用法
  8. 粗谈Android中的对齐
  9. PL/SQL 9.0工具技巧
  10. Swift—下标-备
  11. SDL 在指定窗口中绘图
  12. 【Qt编程】基于Qt的词典开发系列&lt;六&gt;--界面美化设计
  13. bzoj4044 [Cerc2014] Virus synthesis
  14. 1.3 正则表达式和python语言-1.3.7 匹配任何单个字符
  15. 【Alpha】Scrum Meeting 1
  16. 每日linux命令学习-lsattr和chattr
  17. mysql日常处理
  18. 数字统计(NOIP2010)
  19. 面向对象编程(OOP)思想小结
  20. Visual Studio2017如何设置自动生成的代码不换行

热门文章

  1. mysql增加远程连接用户及查看数据库表结构
  2. &lt;cctype&gt;库
  3. django的流程分析
  4. swift - 封装 GCDTimer 和 NSTimer
  5. 获取url路径中的参数
  6. 一定要 先删除 sc表 中的 某元组 行,,, 再删除 course表中的 元组行
  7. OAuth 2.0 学习
  8. 2. Get the codes from GIT
  9. 原生JS 实现元素排序
  10. ubuntu16下Elasticsearch5.1.1安装部署