- 获取时间戳
#region 获取时间戳
/// <summary>
/// 获取时间戳
/// </summary>
/// <returns></returns>
public static string GetTimeStamp()
{
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
return Convert.ToInt64(ts.TotalMilliseconds).ToString(CultureInfo.InvariantCulture);
}
#endregion
- 集合转换为json字符串
#region List<Dictionary>ToJson
/// <summary>
/// List Dictionary ToJson
/// </summary>
/// <param name="jsonName"></param>
/// <param name="list">要转换的对象</param>
/// <returns></returns>
public static string ListDictionaryToJson(string jsonName, List<Dictionary<string, string>> list)
{
StringBuilder json = new StringBuilder();
json.Append("{\"" + jsonName + "\":[");
foreach (var dic in list)
{
json.Append("{");
foreach (var kvp in dic)
{
json.Append("\"" + kvp.Key + "\":\"" + HttpUtility.UrlEncodeUnicode(kvp.Value) + "\",");
}
if (dic.Count > 0)
{
json.Remove(json.Length - 1, 1);
}
json.Append("},");
}
if (list.Count > 0)
{
json.Remove(json.Length - 1, 1);
}
json.Append("]}");
return json.ToString();
}
#endregion
- 删除文件
#region 删除文件
/// <summary>
/// 删除文件
/// </summary>
/// <param name="serverPath">根目录路径</param>
/// <param name="fileName">文件名</param>
public static void DeleteFile(string filePath)
{
if (string.IsNullOrEmpty(filePath))
{
return;
}
if (File.Exists(filePath))
{
FileInfo fi = new FileInfo(filePath);
if (fi.Attributes.ToString().IndexOf("ReadOnly", StringComparison.Ordinal) != -1)
{
fi.Attributes = FileAttributes.Normal;
}
File.Delete(filePath);
}
}
#endregion
- 反序输出字符串
#region 反序输出字符串
/// <summary>
/// 反序输出字符串
/// </summary>
/// <param name="input">需要反序的字符串</param>
/// <returns></returns>
public static string Revert(string input)
{
if (string.IsNullOrEmpty(input)) return "";
string temp = input;
StringBuilder sb = new StringBuilder();
for (int i = temp.Length - 1; i >= 0; i--)
{
char ch = temp[i];
sb.Append(ch);
}
return sb.ToString();
}
#endregion
字符串反序输出更简单的写法:
//字符串反序
char[] encryptInfo = EncryptInfo.ToCharArray();
Array.Reverse(encryptInfo);
string result = new string(encryptInfo);