/// <summary> /// 通过递归获取目录及其子目录下的所有文件 /// </summary> /// <param name="path">文件夹目录</param> /// <returns></returns> public static List <string> GetFilesInDirectory(string path) { List <string> list = new List <string>(); path = path.Replace("\\", "/");; if (!Directory.Exists(path)) { ATLog.Error(path + "Not Found!" + ATStackInfo.GetCurrentStackInfo()); return(null); } //获取下面的所有文件 List <string> files = new List <string>(Directory.GetFiles(path)); list.AddRange(files); //对文件夹进行递归 List <string> folders = new List <string>(Directory.GetDirectories(path)); folders.ForEach(c => { string tempFolderName = Path.Combine(path, Path.GetFileName(c)); list.AddRange(GetFilesInDirectory(tempFolderName)); }); return(list); }
/// <summary> /// 将文件夹下的所有内容全部复制给定目录中 /// 注意:原根会被删除 /// </summary> /// <param name="sPath"></param> /// <param name="tPath"></param> public static bool MoveFolder(string sPath, string tPath) { if (!Directory.Exists(sPath)) { ATLog.Error(sPath + ": Not Found"); return(false); } try { if (Directory.Exists(tPath)) { RemoveFullFolder(tPath); } Directory.Move(sPath, tPath); } catch (Exception ex) { ATLog.Error(ex.Message + "\n" + ex.StackTrace); return(false); } return(true); }
private string ComputeHash(string path) { StringBuilder finalMd5 = new StringBuilder(); MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); using (FileStream fs = new FileStream(path, FileMode.Open)) { byte[] bytes = null; try { bytes = md5.ComputeHash(fs); } catch (Exception ex) { ATLog.Error(ex.Message + "\n" + ex.StackTrace); } finally { md5.Clear(); } foreach (var item in bytes) { finalMd5.Append(item.ToString("x2")); } } return(finalMd5.ToString()); }
/// <summary> /// 枚举的值转化为UInt64 /// </summary> /// <param name="value"></param> /// <returns></returns> private static ulong ToUInt64(Object value) { TypeCode typeCode = Convert.GetTypeCode(value); ulong result; switch (typeCode) { case TypeCode.SByte: case TypeCode.Int16: case TypeCode.Int32: case TypeCode.Int64: result = (UInt64)Convert.ToInt64(value, CultureInfo.InvariantCulture); break; case TypeCode.Byte: case TypeCode.UInt16: case TypeCode.UInt32: case TypeCode.UInt64: case TypeCode.Boolean: case TypeCode.Char: result = Convert.ToUInt64(value, CultureInfo.InvariantCulture); break; default: ATLog.Error("Invalid Object type in ToUInt64"); throw new InvalidOperationException("InvalidOperation_UnknownEnumType"); } return(result); }
/// <summary> /// 枚举中是否包含,相比于Enum.IsDefine,在字符串比较时区分大小写 /// </summary> /// <typeparam name="T">指定的枚举类型</typeparam> /// <param name="enumValue">枚举实例</param> /// <returns></returns> /// enum Permission /// { /// Normal, /// Vip /// } /// Enum.IsDefined(typeof(Permission),"Normal") true /// Enum.IsDefined(typeof(Permission), "normal") false /// ATEnum.Constains<Permission>("normal") true public static bool Constains <T>(object enumValue) { Type valueType = enumValue.GetType(); if (valueType == typeof(string)) { string[] names = ATEnum.GetNames <T>(); string value = enumValue.ToString().ToLower(); for (int i = 0; i < names.Length; i++) { if (value.Equals(names[i].ToLower())) { return(true); } } } if (valueType.IsIntegerType()) { return(ATEnum.BinarySearch <T>(ATEnum.GetValues <T>(), enumValue) >= 0); } ATLog.Error("参数类型错误!" + ATStackInfo.GetCurrentStackInfo()); throw new ArgumentException("参数类型错误!" + ATStackInfo.GetCurrentStackInfo()); }
/// <summary> /// 获取枚举中所有成员名称 /// </summary> /// <typeparam name="T"></typeparam> /// <returns></returns> public static string[] GetNames <T>() { Type type = typeof(T); if (!type.GetTypeInfo().IsEnum) { ATLog.Error("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo()); throw new ArgumentException("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo()); } return(Enum.GetNames(type)); }
/// <summary> /// 复制文件 /// </summary> /// <param name="sourcePath">源文件名</param> /// <param name="destPath">目标文件名</param> public static void CopyFile(string sourcePath, string destPath, bool isrewrite = true) { sourcePath = sourcePath.Replace("\\", "/"); destPath = destPath.Replace("\\", "/"); try { global::System.IO.File.Copy(sourcePath, destPath, isrewrite); } catch (Exception ex) { ATLog.Error(ex); } }
/// <summary> /// 获取实例 /// </summary> /// <typeparam name="T">枚举类型</typeparam> /// <param name="obj">成员名称或值</param> /// /// //声明 /// enum TestEnum /// { /// A = 0 /// } /// /// 示例调用: /// TestEnum type = ATEnum.Parse<TestEnum>("A"); /// /// <returns>obj对应枚举实例中的成员</returns> public static T GetValue <T>(object obj) { Contract.Requires(obj != null); Contract.Requires(Contract.Result <T>() != null); Type type = typeof(T); if (!type.GetTypeInfo().IsEnum) { ATLog.Error("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo()); throw new ArgumentException("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo()); } string value = obj.ToString(); return((T)Enum.Parse(type, value, true)); }
/// <summary> /// 移除文件 /// </summary> /// <param name="path"></param> public static bool RemoveFile(string path) { try { if (global::System.IO.File.Exists(path)) { global::System.IO.File.Delete(path); } } catch (Exception ex) { ATLog.Error(ex.Message + "\n" + ex.StackTrace); return(false); } return(true); }
/// <summary> /// 删除文件夹及其所有子文件 /// </summary> /// <param name="path">文件夹路径</param> public static bool RemoveFullFolder(string path) { path = path.Replace("\\", "/"); if (!Directory.Exists(path)) { ATLog.Error(path + "Not Found"); return(false); } List <string> files = new List <string>(Directory.GetFiles(path)); files.ForEach(c => { string tempFileName = Path.Combine(path, Path.GetFileName(c)); FileInfo fileInfo = new FileInfo(tempFileName); if (fileInfo.Attributes != FileAttributes.Normal) { fileInfo.Attributes = FileAttributes.Normal; } fileInfo.Delete(); ATLog.Info(fileInfo.Name + "已经删除!"); }); List <string> folders = new List <string>(Directory.GetDirectories(path)); folders.ForEach(c => { string tempFolderName = Path.Combine(path, Path.GetFileName(c)); RemoveFullFolder(tempFolderName); }); DirectoryInfo directoryInfo = new DirectoryInfo(path); if (directoryInfo.Attributes != FileAttributes.Normal) { directoryInfo.Attributes = FileAttributes.Normal; } directoryInfo.Delete(); ATLog.Info(directoryInfo.Name + "已经删除!"); return(true); }
/// <summary> /// 用于转换数据类型(实际使用的) /// </summary> /// <param name="configure"></param> public virtual void Convert(Configure <T1> configure) { this.sender = configure.sender; this.receivers = configure.receivers; this.license = configure.license; this.database = configure.database; string[] noticeTimeData = configure.noticetime.Split('-'); try { this.noticeTime = new TimeSpan(int.Parse(noticeTimeData[0]), int.Parse(noticeTimeData[1]), 0); } catch (Exception ex) { ATLog.Error(ex.Message + "\n" + ex.StackTrace); } this.isDebug = configure.debug; this.isDebugSend = false;//初始化为false this.noticeRate = (NoticeRate)Enum.Parse(typeof(NoticeRate), configure.noticerate.ToUpper()); this.mailType = (MailContentType)Enum.Parse(typeof(MailContentType), configure.mailcontenttype.ToUpper()); }
/// <summary> /// 二分法查找值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="array"></param> /// <param name="value"></param> /// <returns></returns> public static int BinarySearch <T>(Array array, object value) { Type type = typeof(T); if (!type.GetTypeInfo().IsEnum) { ATLog.Error("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo()); throw new ArgumentException("泛型参数不是枚举类型:\n" + ATStackInfo.GetCurrentStackInfo()); } ulong[] ulArray = new ulong[array.Length]; for (int i = 0; i < array.Length; i++) { ulArray[i] = ToUInt64(array.GetValue(i)); } ulong ulValue = ToUInt64(value); return(Array.BinarySearch(ulArray, ulValue)); }
/// <summary> /// 向文件中写入内容,若文件不存在,则创建文件 /// </summary> /// <param name="filePath"></param> /// <param name="content"></param> /// <returns></returns> public static string WriteToFile(string filePath, string content, FileMode mode = FileMode.OpenOrCreate) { Console.WriteLine("Name: " + Thread.CurrentThread.Name + " ,content:" + string.IsNullOrWhiteSpace(content)); lock (Sync) { if (!File.Exists(filePath)) { mode = FileMode.OpenOrCreate; } string error = null; FileStream fs = null; StreamWriter sw = null; try { fs = new FileStream(filePath, mode, FileAccess.Write); sw = new StreamWriter(fs); sw.WriteLine(content); } catch (Exception ex) { error = ex.Message; ATLog.Error(ex.Message + "\n" + ex.StackTrace); return(error); } finally { if (sw != null) { sw.Close(); } } return(error); } }
/// <summary> /// 复制文件夹及其子目录、文件到目标文件夹 /// </summary> /// <param name="sourcePath"></param> /// <param name="destPath"></param> public static void CopyFolder(string sourcePath, string destPath) { if (Directory.Exists(sourcePath)) { if (!Directory.Exists(destPath)) { //目标目录不存在则创建 try { Directory.CreateDirectory(destPath); } catch (Exception ex) { ATLog.Error(ex); throw new Exception("create target folder fail...:" + ex.Message); } } List <string> files = new List <string>(Directory.GetFiles(sourcePath)); files.ForEach(c => { string destFile = Path.Combine(destPath, Path.GetFileName(c)); global::System.IO.File.Copy(c, destFile, true); }); List <string> folders = new List <string>(Directory.GetDirectories(sourcePath)); folders.ForEach(c => { string destDir = Path.Combine(destPath, Path.GetFileName(c)); CopyFolder(c, destDir); }); } else { throw new DirectoryNotFoundException("sourcePath: " + "source folder not found!"); } }