工具类
数据转化
public class DataConvert
{
/// <summary>
/// 获得MD5加密字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetMD5String(string str)
{
return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
}
/// <summary>
/// 获得MD5加密字符串
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string GetFileMD5String(string FilePath)
{
if (!File.Exists(FilePath))
{
return "";
}
try
{
using (FileStream get_file = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash_byte = get_md5.ComputeHash(get_file);
string resule = System.BitConverter.ToString(hash_byte);
resule = resule.Replace("-", "");
get_file.Close();
get_file.Dispose();
return resule;
}
}
catch (Exception e)
{
Console.WriteLine(e);
return GetMD5String("");
}
}
/// <summary>
/// 将对象序列化
/// </summary>
/// <param name="info"></param>
/// <returns>返回UTF8格式编码的字节数组</returns>
public static byte[] Serialize(object info)
{
if (info == null)
{
return null;
}
JavaScriptSerializer js = new JavaScriptSerializer();
try
{
string str = js.Serialize(info);
if (str == null)
{
return null;
}
return Encoding.UTF8.GetBytes(str);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <returns></returns>
public static Dictionary<string, object> Dserialize(string strJsonData)
{
if (strJsonData == null || strJsonData == "")
{
return null;
}
JavaScriptSerializer js = new JavaScriptSerializer();
try
{
return js.Deserialize<Dictionary<string, object>>(strJsonData);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.Message);
return null;
}
catch (ArgumentException exec)
{
Console.WriteLine(exec.Message);
return null;
}
}
/// <summary>
/// 反序列化
/// </summary>
/// <returns></returns>
public static Dictionary<string, object> Dserialize(byte[] bufferJsonData)
{
return Dserialize(Encoding.Default.GetString(bufferJsonData));
}
}