足力健后端,使用.netcore版本,合并1个项目使用

FileHelper.cs 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Security.Cryptography;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace System.Utility.File
  11. {
  12. public class FileHelper
  13. {
  14. /// <summary>
  15. /// 获取文件的MD5码
  16. /// </summary>
  17. /// <param name="fileName">传入的文件名(含路径及后缀名)</param>
  18. /// <returns></returns>
  19. public static string GetMD5HashFromFile(string fileName)
  20. {
  21. try
  22. {
  23. FileStream file = new FileStream(fileName, System.IO.FileMode.Open);
  24. MD5 md5 = new MD5CryptoServiceProvider();
  25. byte[] retVal = md5.ComputeHash(file);
  26. file.Close();
  27. StringBuilder sb = new StringBuilder();
  28. for (int i = 0; i < retVal.Length; i++)
  29. {
  30. sb.Append(retVal[i].ToString("x2"));
  31. }
  32. return sb.ToString();
  33. }
  34. catch (Exception ex)
  35. {
  36. throw new Exception("GetMD5HashFromFile() fail,error:" + ex.Message);
  37. }
  38. }
  39. /// <summary>
  40. /// 生成缩略图
  41. /// </summary>
  42. /// <param name="path"></param>
  43. /// <param name="fileName"></param>
  44. /// <returns></returns>
  45. public static string GetThumbImg(string path, string fileName, string fileExt)
  46. {
  47. string[] filetypes = new string[] { ".jpg", ".jpeg", ".gif", ".bmp", ".png" };
  48. if (filetypes.Contains(fileExt))
  49. {
  50. FileStream file = new FileStream(path + fileName, System.IO.FileMode.Open);
  51. //获取图片的高度和宽度
  52. System.Drawing.Image Img = System.Drawing.Image.FromStream(file);
  53. int _Width = Img.Width;
  54. int _Height = Img.Height;
  55. int _sHeight = 120;//设置生成缩略图的高度
  56. int _sWidth = 120;//设置生成缩略图的宽度
  57. #region 缩略图大小只设置了最大范围,并不是实际大小
  58. float realbili = (float)_Width / (float)_Height;
  59. float wishbili = (float)_sWidth / (float)_sHeight;
  60. //实际图比缩略图最大尺寸更宽矮,以宽为准
  61. if (realbili > wishbili)
  62. {
  63. _sHeight = (int)((float)_sWidth / realbili);
  64. }
  65. //实际图比缩略图最大尺寸更高长,以高为准
  66. else
  67. {
  68. _sWidth = (int)((float)_sHeight * realbili);
  69. }
  70. #endregion
  71. string[] filenames = fileName.Split('.');
  72. string OutThumbFileName = filenames[0].ToString() + "_s." + filenames[1].ToString();
  73. string ImgFilePath = path + OutThumbFileName;
  74. System.Drawing.Image newImg = Img.GetThumbnailImage(_sWidth, _sHeight, null, System.IntPtr.Zero);
  75. newImg.Save(ImgFilePath);
  76. newImg.Dispose();
  77. Img.Dispose();
  78. return OutThumbFileName;
  79. }
  80. else
  81. {
  82. return "";
  83. }
  84. }
  85. //判断文件夹是否存在
  86. public static bool FolderExits(string path)
  87. {
  88. DirectoryInfo dir = new DirectoryInfo(path);
  89. if (dir.Exists)//文件夹存在
  90. {
  91. return true;
  92. }
  93. else
  94. {
  95. //dir.Create();//不存在就创建一个
  96. return false;
  97. }
  98. }
  99. //创建一个文件夹,存在就创建失败
  100. public static bool CreateNewFolder(string path)
  101. {
  102. DirectoryInfo dir = new DirectoryInfo(path);
  103. if (!dir.Exists)
  104. {
  105. dir.Create();
  106. return true;
  107. }
  108. else
  109. return false;
  110. }
  111. /// <summary>
  112. /// 在指定目录下创建指定名称文件夹
  113. /// </summary>
  114. /// <param name="ParentsPath"></param>
  115. /// <param name="NewFolderName"></param>
  116. /// <returns></returns>
  117. public static bool CreateNewFolder(string ParentsPath, string NewFolderName)
  118. {
  119. string CreatePath = ParentsPath + @"\" + NewFolderName;
  120. DirectoryInfo dir = new DirectoryInfo(CreatePath);
  121. if (!dir.Exists)
  122. {
  123. dir.Create();
  124. return true;
  125. }
  126. else
  127. return false;
  128. }
  129. /// <summary>
  130. /// 返回目录下的所有文件名
  131. /// </summary>
  132. /// <param name="path"></param>
  133. /// <returns></returns>
  134. public static ArrayList getAllFiles(string path)
  135. {
  136. DirectoryInfo dir = new DirectoryInfo(path);
  137. if (dir.Exists)
  138. {
  139. FileInfo[] fileinfo = dir.GetFiles();
  140. ArrayList list = new ArrayList();
  141. foreach (FileInfo f in fileinfo)
  142. {
  143. list.Add(f.Name);
  144. }
  145. return list;
  146. }
  147. else
  148. return null;
  149. }
  150. /// <summary>
  151. /// 计算文件夹的大小
  152. /// </summary>
  153. /// <param name="d"></param>
  154. /// <returns></returns>
  155. public static long DirSize(DirectoryInfo d)
  156. {
  157. long Size = 0;
  158. // Add file sizes.
  159. FileInfo[] fis = d.GetFiles();//获得目录文件列表
  160. foreach (FileInfo fi in fis)
  161. {
  162. Size += fi.Length;
  163. }
  164. // Add subdirectory sizes.
  165. DirectoryInfo[] dis = d.GetDirectories();//获取目录子目录列表
  166. foreach (DirectoryInfo di in dis)
  167. {
  168. Size += DirSize(di);
  169. }
  170. return Size;
  171. }
  172. /// <summary>
  173. /// 把文件夹得大小转换成比较合适的表示单位
  174. /// </summary>
  175. /// <param name="size"></param>
  176. /// <returns></returns>
  177. public static string ViewSize(long size)
  178. {
  179. long m = size;
  180. string viewstr;
  181. if ((m / 1024) > 0)//表示可以转换成KB
  182. {
  183. m = m / 1024;//转换成KB
  184. if ((m / 1024) > 0)//表示可以转换成MB
  185. {
  186. m = m / 1024;//转换成MB了
  187. if ((m / 1024) > 0)//表示可以转换成GB
  188. {
  189. m = m / 1024;//转换成GB了
  190. viewstr = m.ToString() + "GB";
  191. }
  192. else
  193. {
  194. viewstr = m.ToString() + "MB";
  195. }
  196. }
  197. else
  198. {
  199. viewstr = m.ToString() + "KB";
  200. }
  201. }
  202. else
  203. {
  204. viewstr = m.ToString() + "byte";
  205. }
  206. return viewstr;
  207. }
  208. /// <summary>
  209. /// 删除指定目录和内容
  210. /// </summary>
  211. /// <param name="dir"></param>
  212. /// <returns></returns>
  213. public static bool delDir(string dir)
  214. {
  215. bool flag = false;
  216. DirectoryInfo d = new DirectoryInfo(dir);
  217. if (d.Exists)//判断目录是否存在
  218. {
  219. try
  220. {
  221. d.Delete();
  222. flag = true;
  223. }
  224. catch (Exception e) { flag = false; }
  225. }
  226. return flag;
  227. }
  228. /// <summary>
  229. /// 删除指定文件
  230. /// </summary>
  231. /// <param name="fil"></param>
  232. /// <returns></returns>
  233. public static bool delFile(string fil)
  234. {
  235. bool flag = false;
  236. FileInfo d = new FileInfo(fil);
  237. if (d.Exists)//判断目录是否存在
  238. {
  239. try
  240. {
  241. d.Delete();
  242. flag = true;
  243. }
  244. catch (Exception e) { flag = false; }
  245. }
  246. return flag;
  247. }
  248. /// <summary>
  249. /// 判断文件是否存在
  250. /// </summary>
  251. /// <param name="fil"></param>
  252. /// <returns></returns>
  253. public static bool FileExists(string fil)
  254. {
  255. bool flag = false;
  256. FileInfo d = new FileInfo(fil);
  257. return d.Exists;
  258. }
  259. public static void Copy(string sourceDirectory, string targetDirectory)
  260. {
  261. DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
  262. DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
  263. CopyAll(diSource, diTarget);
  264. }
  265. /// <summary>
  266. /// 复制目录及子文件到指定目录
  267. /// </summary>
  268. /// <param name="source"></param>
  269. /// <param name="target"></param>
  270. public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
  271. {
  272. // Check if the target directory exists, if not, create it.
  273. //if (FolderExits(target.FullName) == false)
  274. //{
  275. // Directory.CreateDirectory(target.FullName);
  276. //}
  277. CreateNewFolder(target.FullName);
  278. // Copy each file into it's new directory.
  279. foreach (FileInfo fi in source.GetFiles())
  280. {
  281. Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
  282. fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
  283. }
  284. // Copy each subdirectory using recursion.
  285. foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
  286. {
  287. DirectoryInfo nextTargetSubDir =
  288. target.CreateSubdirectory(diSourceSubDir.Name);
  289. CopyAll(diSourceSubDir, nextTargetSubDir);
  290. }
  291. }
  292. /// <summary>
  293. /// 循环读取某个目录下的所有文件和目录,查询有关每一项的一些信息。返回一个文件列表
  294. /// </summary>
  295. /// <param name="strCurrentDir"></param>
  296. public static List<fileEntity> FileView(string strCurrentDir)
  297. {
  298. List<fileEntity> fileList = new List<fileEntity>();
  299. DirectoryInfo dir = new DirectoryInfo(strCurrentDir);
  300. foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())//这个循环再读取文件的信息
  301. {
  302. try
  303. {
  304. //FileSystemInfo 对象可以表示文件或目录,从而可以作为 FileInfo 或 DirectoryInfo 对象的基础。 当分析许多文件和目录时,请使用该基类。
  305. FileInfo fi;
  306. DirectoryInfo di;
  307. //创建一个自己写的实体类的实体
  308. fileEntity newfile = new fileEntity();
  309. if (fsi is FileInfo)//外层循环读取文件信息
  310. {
  311. //表示当前fsi是文件
  312. fi = (FileInfo)fsi;
  313. newfile.fileName = fi.Name;
  314. newfile.fileExt = fi.Extension;
  315. newfile.fileSize = fi.Length;
  316. newfile.FileModify = fi.LastWriteTime;
  317. //通过扩展名来选择文件显示图标
  318. switch (newfile.fileExt)
  319. {
  320. case ".gif":
  321. newfile.picName = "gif.gif";
  322. break;
  323. default:
  324. newfile.picName = "other.gif";
  325. break;
  326. }
  327. newfile.picName = "<img src='" + newfile.picName + "' width=25 height=20>";
  328. }
  329. else
  330. {
  331. di = (DirectoryInfo)fsi;
  332. newfile.fileName = di.Name;
  333. newfile.fileSize = DirSize(di);//调用计算文件夹大小的方法
  334. newfile.FileModify = di.LastWriteTime;
  335. newfile.picName = "<img src='directory.gif' width=25 height=20>";
  336. }
  337. fileList.Add(newfile);
  338. }
  339. catch (Exception e) { }
  340. }
  341. return fileList;
  342. }
  343. /// <summary>
  344. /// 显示目录和文件
  345. /// </summary>
  346. /// <param name="path"></param>
  347. public static void All(string path)
  348. {
  349. FileInfo fi;//文件
  350. DirectoryInfo di;//目录
  351. DirectoryInfo dir = null;
  352. int i = 0; //控制行的颜色
  353. try
  354. {
  355. dir = new DirectoryInfo(path);
  356. }
  357. catch (Exception e) { }
  358. foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())
  359. {
  360. try
  361. {
  362. fileEntity newfile = new fileEntity();
  363. FolderEntity folder = new FolderEntity();
  364. newfile.fileName = "";
  365. newfile.picName = "";
  366. newfile.fileExt = "";
  367. newfile.fileSize = 0;
  368. folder.folderName = "";
  369. folder.picName = "";
  370. i += 1;
  371. if (fsi is FileInfo)//判断当前读取的是不是一个文件
  372. {
  373. //表示当前fsi是文件
  374. fi = (FileInfo)fsi;
  375. newfile.fileName = fi.Name;
  376. newfile.fileExt = fi.Extension;
  377. newfile.fileSize = fi.Length;
  378. newfile.FileModify = fi.LastWriteTime;
  379. //将文件加上可以下载文件的链接
  380. newfile.fileName = "<a href='........'></a>";
  381. //通过扩展名来选择文件显示图标
  382. //Response.Write(Session["DataBasePath"].ToString()+"\\filetype\\"+pfun.getFileExt(FileExt)+".gif");
  383. if (fsi.Exists)
  384. {
  385. switch (newfile.fileExt)
  386. {
  387. case ".gif":
  388. newfile.picName = "gif.gif";
  389. break;
  390. default:
  391. newfile.picName = "other.gif";
  392. break;
  393. }
  394. }
  395. else
  396. {
  397. newfile.picName = "unknown.gif";
  398. }
  399. /*
  400. switch(FileExt)
  401. {
  402. case ".gif":
  403. FilePic = "gif.gif";
  404. break;
  405. default:
  406. FilePic = "other.gif";
  407. break;
  408. }
  409. */
  410. newfile.picName = "<img src='filetype/" + newfile.picName + "' width=16 height=16>";
  411. }
  412. else
  413. {
  414. //当前为目录
  415. di = (DirectoryInfo)fsi;
  416. folder.folderName = di.Name;
  417. //给目录加上链接
  418. folder.folderName = "<a href='.......'><a>";
  419. folder.lastTime = di.LastWriteTime;
  420. folder.picName = "<img src='filetype/folder.gif' width=16 height=16>";
  421. }
  422. }
  423. catch (Exception e) { }
  424. }
  425. }
  426. }
  427. public class fileEntity
  428. {
  429. public string fileName { get; set; }
  430. public string fileExt { get; set; }
  431. public long fileSize { get; set; }
  432. public DateTime FileModify { get; set; }
  433. public string picName { get; set; }
  434. }
  435. public class FolderEntity
  436. {
  437. public string folderName { get; set; }
  438. public DateTime lastTime { get; set; }
  439. public string picName { get; set; }
  440. }
  441. }