示例#1
0
        /// <summary>获取文件的md5校验码</summary>
        public static string GetMD5HashFromFile(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                return(null);
            }
            try
            {
                if (File.Exists(fileName))
                {
                    FileStream file   = File.OpenRead(fileName);
                    MD5        md5    = new MD5CryptoServiceProvider();
                    byte[]     retVal = md5.ComputeHash(file);
                    file.Close();
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < retVal.Length; i++)
                    {
                        sb.Append(retVal[i].ToString("x2"));
                    }
                    return(sb.ToString());
                }

                return(null);
            }
            catch (Exception e)
            {
                DSLog.E(e.ToString(), false);
                return(null);
            }
        }
示例#2
0
        /// <summary>采用GBK编码缩减字符串到指定字节长度,超出部分使用指定文本代替</summary>
        public static string ReduceStringToLength(string str, int length, string replaceTxt = "...")
        {
            try
            {
                int byteCount = GetStringLengthByGBK(str);
                if (byteCount > length)
                {
                    length -= 2; //超出指定长度,则缩减2个字节,使用"..."结尾
                    while (byteCount > length)
                    {
                        str       = str.Substring(0, str.Length - 1);
                        byteCount = GetStringLengthByGBK(str);
                    }

                    return(str + replaceTxt);
                }
                else
                {
                    return(str);
                }
            }
            catch (Exception e)
            {
                DSLog.I(e.ToString());
                return(str);
            }
        }
示例#3
0
 /// <summary> 在指定目录下创建二进制文件</summary>
 public static bool CreateBytesFile(string filePath, byte[] bytes)
 {
     try
     {
         string directory = Path.GetDirectoryName(filePath);
         if (!Directory.Exists(directory))
         {
             Directory.CreateDirectory(directory);
         }
         File.WriteAllBytes(filePath, bytes);
         return(true);
     }
     catch (Exception e)
     {
         DSLog.E("创建文件失败:" + e.ToString(), false);
         return(false);
     }
 }