Utils.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Security.Cryptography;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Xml;
  9. namespace Common
  10. {
  11. public class Utils
  12. {
  13. #region 将XML内容转换成目标对象实体集合
  14. /// <summary>
  15. /// 将XML内容转换成目标对象实体集合
  16. /// </summary>
  17. /// <typeparam name="T">目标对象实体</typeparam>
  18. /// <param name="fileName">完整文件名(根目录下只需文件名称)</param>
  19. /// <param name="wrapperNodeName">主节点名称</param>
  20. /// <returns></returns>
  21. public static List<T> ConvertXmlToObject<T>(string fileName, string wrapperNodeName)
  22. {
  23. XmlDocument doc = new XmlDocument();
  24. doc.Load(fileName);
  25. List<T> result = new List<T>();
  26. Type type = typeof(T);
  27. XmlNodeList nodeList = doc.ChildNodes;
  28. if (!string.IsNullOrEmpty(wrapperNodeName))
  29. {
  30. foreach (XmlNode node in doc.ChildNodes)
  31. {
  32. if (node.Name == wrapperNodeName)
  33. {
  34. nodeList = node.ChildNodes;
  35. break;
  36. }
  37. }
  38. }
  39. object oneT = null;
  40. foreach (XmlNode node in nodeList)
  41. {
  42. if (node.NodeType == XmlNodeType.Comment || node.NodeType == XmlNodeType.XmlDeclaration) continue;
  43. oneT = type.Assembly.CreateInstance(type.FullName ?? throw new InvalidOperationException());
  44. foreach (XmlNode item in node.ChildNodes)
  45. {
  46. if (item.NodeType == XmlNodeType.Comment) continue;
  47. var property = type.GetProperty(item.Name);
  48. if (property != null)
  49. property.SetValue(oneT, Convert.ChangeType(item.InnerText, property.PropertyType), null);
  50. }
  51. result.Add((T)oneT);
  52. }
  53. return result;
  54. }
  55. #endregion
  56. #region 获取基目录
  57. /// <summary>
  58. /// 获取基目录
  59. /// </summary>
  60. /// <returns></returns>
  61. public static string GetBaseDirectory(string path)
  62. {
  63. try
  64. {
  65. return AppDomain.CurrentDomain.BaseDirectory + path;
  66. }
  67. catch (Exception)
  68. {
  69. return Path.GetFullPath(path);
  70. }
  71. }
  72. #endregion
  73. #region 从文件的绝对路径中获取除开文件名以及扩展名的部分
  74. /// <summary>
  75. /// 从文件路径中获取除开文件名以及扩展名的部分
  76. /// </summary>
  77. /// <param name="filePath">文件的绝对路径</param>
  78. public static string GetFilePathNoExtensionAndFileName(string filePath)
  79. {
  80. FileInfo fi = new FileInfo(filePath);
  81. return fi.DirectoryName;
  82. }
  83. #endregion
  84. #region 创建一个目录
  85. /// <summary>
  86. /// 创建一个目录
  87. /// </summary>
  88. /// <param name="directoryPath">目录的绝对路径</param>
  89. public static void CreateDirectory(string directoryPath)
  90. {
  91. //如果目录不存在则创建该目录
  92. if (!IsExistDirectory(directoryPath))
  93. {
  94. Directory.CreateDirectory(directoryPath);
  95. }
  96. }
  97. #endregion
  98. #region 检测指定目录是否存在
  99. /// <summary>
  100. /// 检测指定目录是否存在
  101. /// </summary>
  102. /// <param name="directoryPath">目录的绝对路径</param>
  103. /// <returns></returns>
  104. public static bool IsExistDirectory(string directoryPath)
  105. {
  106. return Directory.Exists(directoryPath);
  107. }
  108. #endregion
  109. #region 检测指定文件是否存在,如果存在返回true
  110. /// <summary>
  111. /// 检测指定文件是否存在,如果存在则返回true。
  112. /// </summary>
  113. /// <param name="filePath">文件的绝对路径</param>
  114. public static bool IsExistFile(string filePath)
  115. {
  116. return System.IO.File.Exists(filePath);
  117. }
  118. #endregion
  119. #region 删除文件
  120. /// <summary>
  121. /// 删除文件
  122. /// </summary>
  123. /// <param name="filepath">要删除的文件路径和名称</param>
  124. public static void DeleteFile(string filepath)
  125. {
  126. if (IsExistFile(filepath))
  127. {
  128. System.IO.File.Delete(filepath);
  129. }
  130. }
  131. #endregion
  132. #region 移动文件(剪贴--粘贴)
  133. /// <summary>
  134. /// 移动文件(剪贴--粘贴)
  135. /// </summary>
  136. /// <param name="dir1">要移动的文件的路径及全名(包括后缀)</param>
  137. /// <param name="dir2">文件移动到新的位置,并指定新的文件名</param>
  138. public static void MoveFile(string dir1, string dir2)
  139. {
  140. dir1 = dir1.Replace("/", "\\");
  141. dir2 = dir2.Replace("/", "\\");
  142. if (IsExistFile(dir1))
  143. {
  144. File.Move(dir1, dir2);
  145. }
  146. }
  147. #endregion
  148. #region 复制文件
  149. /// <summary>
  150. /// 复制文件
  151. /// </summary>
  152. /// <param name="dir1">要复制的文件的路径已经全名(包括后缀)</param>
  153. /// <param name="dir2">目标位置,并指定新的文件名</param>
  154. public static void CopyFile(string dir1, string dir2)
  155. {
  156. dir1 = dir1.Replace("/", "\\");
  157. dir2 = dir2.Replace("/", "\\");
  158. if (IsExistFile(dir1))
  159. {
  160. System.IO.File.Copy(dir1, dir2, true);
  161. }
  162. }
  163. #endregion
  164. }
  165. }