FileOperationHelper.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. using PaintDotNet.Base.Functionodel;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Security.Cryptography;
  9. using System.Security.Principal;
  10. using System.Text;
  11. using System.Windows.Forms;
  12. using static PaintDotNet.Base.Functionodel.ZipXmlModel;
  13. namespace PaintDotNet.Base.CommTool
  14. {
  15. /// <summary>
  16. /// 文件公共处理类
  17. /// </summary>
  18. public static class FileOperationHelper
  19. {
  20. public static string privateKey = "<RSAKeyValue><Modulus>k44zKgQwvGWgeyLzgBpY/0/PIX2MsWz2umEx53G74tz14rmplqtOozM+42TqZgTh3sX/iDTxa/rfhQizZmYIe3HvxkG7F5CGjnNTreQzFZM7odnHtHPK5LZTkXCm8kob6xlZd5w8A+Y5rlWas4dXpSquhEfGCGGENjydWT6Px+U=</Modulus><Exponent>AQAB</Exponent><P>z7F5KyVFLY9qRz4vp5JKTzN510O2csWI8uvTCx8DAt9jkJzTOGJYiFrlT0OIDDujF03fTCwkuBN9JObgAul6Xw==</P><Q>td/8jhHrSmCaunHwPYooDnwbhgnW1ZiXavZp+lXVAcZrIoGkzQUiSI2N1c6Jj2vPy0JYcYlGRsTQKJ0Nsa/sOw==</Q><DP>Ovh5HvcGHVmLI49UmI/A6ZwEDEr9krjjmZW75nx3rmkfLABbOLczzAOC+G6EQnTsacGClW4zPtDJx6CGGk2QoQ==</DP><DQ>RFGRIyTkB5pmROcL4XIGPfqstBr6El4xcsKBaMHZM8N+9wVQDJuDF1HlF41v6uoKskWHx45TUb4Ym0jzne2BhQ==</DQ><InverseQ>agTNLFUFg7tFIA6wOS30JP0wCQSinrWuDRl5SK1l55Rv2ssnWITDBd+5T9IuQwCqu8AWCm2zb06bkppBkY44dw==</InverseQ><D>GsLZiK9F34VW+741B3C/314sJNjOYYdvoBHsqRs5hkWo2rvthAQBuRucNkWhNWuBQ5QJajyf5IOVcl1HnDS5KNOzr0tzEMCm3LqdfS1cf7FAeytAREZd7aZUt4jRh249o5zu6lgCp/ITz3ZX8XX7eMMXq64GoWS55P8oTxawdqk=</D></RSAKeyValue>";
  21. /// <summary>
  22. /// 读取文件内容到字符串
  23. /// </summary>
  24. /// <param name="filepath">文件路径</param>
  25. /// <param name="mode">操作模式</param>
  26. /// <returns></returns>
  27. public static string ReadStringFromFile(string filepath, FileMode mode)
  28. {
  29. try
  30. {
  31. using (FileStream fs = new FileStream(filepath, mode))
  32. {
  33. StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));
  34. return sr.ReadToEnd().Trim();
  35. }
  36. }
  37. catch (Exception ex)
  38. {
  39. throw new Exception("",ex);// "文件读取失败", ex);
  40. }
  41. }
  42. /// <summary>
  43. /// 将字符串写入到文件
  44. /// </summary>
  45. /// <param name="strXML">字符串</param>
  46. /// <param name="filePath">文件路径</param>
  47. /// <param name="mode">文件操作模式</param>
  48. /// <returns></returns>
  49. public static bool WriteStringToFile(string strXML, string filePath, FileMode mode)
  50. {
  51. try
  52. {
  53. if (File.Exists(filePath))
  54. {
  55. File.Delete(filePath);
  56. }
  57. FileStream fs = new FileStream(filePath, mode);
  58. byte[] data = System.Text.Encoding.UTF8.GetBytes(strXML);
  59. //开始写入
  60. fs.Write(data, 0, data.Length);
  61. //清空缓冲区、关闭流
  62. fs.Flush();
  63. fs.Close();
  64. return true;
  65. }
  66. catch (Exception ex)
  67. {
  68. throw new Exception("", ex); //("文件写入失败", ex);
  69. }
  70. }
  71. /// <summary>
  72. /// 获取文件夹下所有Xml文件名
  73. /// </summary>
  74. /// <param name="path"></param>
  75. /// <returns></returns>
  76. public static List<string> GetFileList(string path) {
  77. DirectoryInfo folder = new DirectoryInfo(path);
  78. List<string> files = new List<string>();
  79. foreach (FileInfo file in folder.GetFiles("*.xml"))
  80. {
  81. files.Add(file.Name);
  82. }
  83. return files;
  84. }
  85. /// <summary>
  86. /// 获取文件夹下所有文件名
  87. /// </summary>
  88. /// <param name="path"></param>
  89. /// <returns></returns>
  90. public static List<string> GetAllFileList(string path)
  91. {
  92. DirectoryInfo folder = new DirectoryInfo(path);
  93. List<string> files = new List<string>();
  94. foreach (FileInfo file in folder.GetFiles())
  95. {
  96. files.Add(file.Name);
  97. }
  98. return files;
  99. }
  100. /// <summary>
  101. /// 获取文件夹下所有文件夹名
  102. /// </summary>
  103. /// <param name="path"></param>
  104. /// <returns></returns>
  105. public static List<string> GetAllDirectoryList(string path)
  106. {
  107. DirectoryInfo folder = new DirectoryInfo(path);
  108. List<string> files = new List<string>();
  109. foreach (DirectoryInfo dir in folder.GetDirectories())
  110. {
  111. files.Add(dir.Name);
  112. }
  113. return files;
  114. }
  115. /// <summary>
  116. /// 文件重命名,直到不重名为止
  117. /// </summary>
  118. /// <param name="path"></param>
  119. /// <returns></returns>
  120. public static string GetReNameFile(string path)
  121. {
  122. string newName;
  123. int i = 1;
  124. do
  125. {
  126. newName = "";
  127. newName += Path.GetDirectoryName(path);
  128. newName += "\\" + Path.GetFileNameWithoutExtension(path) + "-" + i + Path.GetExtension(path);
  129. i++;
  130. }
  131. while (File.Exists(newName));
  132. return newName;
  133. }
  134. /// <summary>
  135. /// 获取文件夹内
  136. /// 当前重名文件之后,还有多少个重名的文件
  137. /// </summary>
  138. /// <param name="fileNames"></param>
  139. /// <param name="i"></param>
  140. /// <returns></returns>
  141. public static int GetNumAfterThisFile(string[] fileNames, int i)
  142. {
  143. int num = 0;
  144. for (int y=0; y<i; y++)
  145. {
  146. //if (y>(i - (fileNames.Length - 1)))
  147. {
  148. if (File.Exists(fileNames[y]))
  149. {
  150. num++;
  151. }
  152. }
  153. }
  154. return num;
  155. }
  156. /// <summary>
  157. /// 目录下所有内容复制
  158. /// </summary>
  159. /// <param name="SourcePath">要Copy的文件夹</param>
  160. /// <param name="DestinationPath">要复制到哪个地方</param>
  161. /// <param name="overwriteexisting">是否覆盖</param>
  162. /// <returns></returns>
  163. public static bool CopyDirectory(string SourcePath, string DestinationPath, bool overwriteexisting)
  164. {
  165. bool ret = false;
  166. try
  167. {
  168. SourcePath = SourcePath.EndsWith(@"\") ? SourcePath : SourcePath + @"\";
  169. DestinationPath = DestinationPath.EndsWith(@"\") ? DestinationPath : DestinationPath + @"\";
  170. if (Directory.Exists(SourcePath))
  171. {
  172. if (Directory.Exists(DestinationPath) == false)
  173. Directory.CreateDirectory(DestinationPath);
  174. foreach (string fls in Directory.GetFiles(SourcePath))
  175. {
  176. FileInfo flinfo = new FileInfo(fls);
  177. flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
  178. }
  179. foreach (string drs in Directory.GetDirectories(SourcePath))
  180. {
  181. DirectoryInfo drinfo = new DirectoryInfo(drs);
  182. if (CopyDirectory(drs, DestinationPath + drinfo.Name, overwriteexisting) == false)
  183. ret = false;
  184. }
  185. }
  186. else {
  187. //复制单个文件用的
  188. SourcePath = SourcePath.EndsWith(@"\") ? SourcePath.Substring(0, SourcePath.Length - 1) : SourcePath;
  189. if (File.Exists(SourcePath))
  190. {
  191. FileInfo flinfo = new FileInfo(SourcePath);
  192. flinfo.CopyTo(DestinationPath + flinfo.Name, overwriteexisting);
  193. }
  194. }
  195. ret = true;
  196. }
  197. catch (Exception)
  198. {
  199. DeleteFolder(DestinationPath);
  200. ret = false;
  201. }
  202. return ret;
  203. }
  204. /// <summary>
  205. /// 删除目录及内部所有文件
  206. /// </summary>
  207. /// <param name="dir">要删除的目录</param>
  208. public static void DeleteFolder(string dir)
  209. {
  210. try
  211. {
  212. if (System.IO.Directory.Exists(dir))
  213. {
  214. string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(dir);
  215. for (int i = 0; i < fileSystemEntries.Length; i++)
  216. {
  217. string text = fileSystemEntries[i];
  218. if (System.IO.File.Exists(text))
  219. {
  220. System.IO.File.Delete(text);
  221. }
  222. else
  223. {
  224. DeleteFolder(text);
  225. }
  226. }
  227. System.IO.Directory.Delete(dir);
  228. }
  229. if (System.IO.File.Exists(dir)) {
  230. System.IO.File.Delete(dir);
  231. }
  232. }
  233. catch (Exception)
  234. {
  235. }
  236. }
  237. /// <summary>
  238. /// 判断指定文件夹及子文件夹内是否有文件正被打开
  239. /// 1.文件夹内有文件被打开 0.没有 -1.文件夹不存在
  240. /// </summary>
  241. /// <param name="path"></param>
  242. /// <returns></returns>
  243. public static int IsFileOpened(string dir)
  244. {
  245. if (System.IO.Directory.Exists(dir))
  246. {
  247. string[] fileSystemEntries = System.IO.Directory.GetFileSystemEntries(dir);
  248. for (int i = 0; i < fileSystemEntries.Length; i++)
  249. {
  250. string text = fileSystemEntries[i];
  251. if (System.IO.File.Exists(text))
  252. {
  253. try
  254. {
  255. System.IO.File.Move(text, text);
  256. }
  257. catch (Exception)
  258. {
  259. return 1;
  260. }
  261. }
  262. else
  263. {
  264. //递归时外层要先接收内层的结果,无法直接多层catch出去
  265. if (IsFileOpened(text) == 1)
  266. return 1;
  267. }
  268. }
  269. return 0;
  270. }
  271. return -1;
  272. }
  273. /// <summary>
  274. /// 计算文件的大小
  275. /// </summary>
  276. /// <param name="lengthOfDocument"></param>
  277. /// <returns></returns>
  278. public static string GetLength(long lengthOfDocument)
  279. {
  280. if (lengthOfDocument < 1024)
  281. return string.Format(lengthOfDocument.ToString() + 'B');
  282. else if (lengthOfDocument > 1024 && lengthOfDocument <= Math.Pow(1024, 2))
  283. return string.Format(Math.Floor(lengthOfDocument / 1024.0).ToString() + "KB");
  284. else if (lengthOfDocument > Math.Pow(1024, 2) && lengthOfDocument <= Math.Pow(1024, 3))
  285. return string.Format(Math.Round(lengthOfDocument / 1024.0 / 1024.0, 2).ToString() + "M");
  286. else
  287. return string.Format(Math.Round(lengthOfDocument / 1024.0 / 1024.0 / 1024.0, 2).ToString() + "GB");
  288. }
  289. /// <summary>
  290. /// 判断文件是word还是excel
  291. /// 1.word; 2.excel; 0.其他
  292. /// </summary>
  293. /// <param name="filePath"></param>
  294. /// <returns></returns>
  295. public static int IsFileWordOrExcel(string filePath)
  296. {
  297. if (!string.IsNullOrEmpty(filePath))
  298. {
  299. string fileExtension = Path.GetExtension(filePath);
  300. if (fileExtension == ".docx" || fileExtension == ".doc")
  301. return 1;
  302. else if (fileExtension == ".xls" || fileExtension == ".xlsx")
  303. return 2;
  304. else
  305. return 0;
  306. }
  307. else
  308. return 0;
  309. }
  310. /// <summary>
  311. /// 获取文件的创建时间
  312. /// </summary>
  313. /// <param name="filePath"></param>
  314. /// <returns></returns>
  315. public static string GetFileCreationTime(string filePath)
  316. {
  317. FileInfo fi = new FileInfo(filePath);
  318. if (fi != null)
  319. return fi.LastWriteTime.ToString();//创建时间同一分钟内可能会相同,不清楚机制,暂时调整为修改时间;
  320. else
  321. return "";
  322. }
  323. /// <summary>
  324. /// 判断文件夹内是否有同名文件
  325. /// </summary>
  326. /// <param name="name"></param>
  327. /// <param name="filePath"></param>
  328. /// <returns></returns>
  329. public static bool IsFileNameExist(string name, string filePath)
  330. {
  331. if (System.IO.Directory.Exists(filePath))
  332. {
  333. DirectoryInfo theFolder = new DirectoryInfo(filePath);
  334. FileInfo[] fileList = theFolder.GetFiles();
  335. foreach (FileInfo fileItem in fileList)
  336. {
  337. if (Path.GetFileNameWithoutExtension(fileItem.FullName) == name)
  338. return true;
  339. }
  340. }
  341. return false;
  342. }
  343. /// <summary>
  344. /// 图片备份-本地图片
  345. /// </summary>
  346. /// <returns></returns>
  347. public static bool BackupImages(string dir, ZipXmlModel zipXmlModel)
  348. {
  349. string backupDict = Application.StartupPath + "\\BackupImages";//固定的图片备份文件夹
  350. if (!Directory.Exists(backupDict))
  351. Directory.CreateDirectory(backupDict);
  352. if (Directory.Exists(dir))//传过来的是文件夹
  353. {
  354. string[] fileNames = Directory.GetFiles(dir);
  355. if (fileNames.Count() == 0)
  356. return false;
  357. //判断是否是图片对象
  358. List<string> imageList = new List<string>();
  359. foreach (string fileName in fileNames)
  360. {
  361. try
  362. {
  363. //Image image = Image.FromFile(fileName);
  364. imageList.Add(fileName);
  365. }
  366. catch (Exception)
  367. {
  368. }
  369. }
  370. if (imageList.Count == 0)
  371. return false;
  372. else
  373. {
  374. ////ZipXmlModel zipXmlModel = new ZipXmlModel();
  375. //if (System.IO.File.Exists(Application.StartupPath + "\\BackupImages\\backupImages.xml"))
  376. // zipXmlModel = XmlSerializeHelper.DESerializer<ZipXmlModel>(ReadStringFromFile(Application.StartupPath + "\\BackupImages\\backupImages.xml", FileMode.Open));
  377. if (zipXmlModel.picNameList == null)
  378. zipXmlModel.picNameList = new List<PicName>();
  379. foreach (string imageName in imageList)
  380. {
  381. string nowName = backupDict + "\\" + Path.GetFileName(imageName);
  382. if (File.Exists(nowName))
  383. {
  384. nowName = GetReNameFile(nowName);
  385. }
  386. try
  387. {
  388. File.Copy(imageName, nowName, true);
  389. }
  390. catch(Exception)
  391. {
  392. continue;
  393. }
  394. //图片名称添加到xml中
  395. PicName onePicName = new PicName();
  396. onePicName.name = nowName;
  397. zipXmlModel.picNameList.Add(onePicName);
  398. }
  399. string xmlNotes = XmlSerializeHelper.XmlSerialize<ZipXmlModel>(zipXmlModel);
  400. WriteStringToFile(xmlNotes, Application.StartupPath + "\\BackupImages\\backupImages.xml", FileMode.Create);
  401. return true;
  402. }
  403. }
  404. else
  405. {
  406. if (System.IO.File.Exists(dir))//传过来的是单独文件
  407. {
  408. try
  409. {
  410. string nowName = backupDict + "\\" + Path.GetFileName(dir);
  411. if (File.Exists(nowName))
  412. {
  413. nowName = GetReNameFile(nowName);
  414. }
  415. File.Copy(dir, nowName, true);
  416. //ZipXmlModel zipXmlModel = new ZipXmlModel();
  417. //if (System.IO.File.Exists(Application.StartupPath + "\\BackupImages\\backupImages.xml"))
  418. // zipXmlModel = XmlSerializeHelper.DESerializer<ZipXmlModel>(ReadStringFromFile(Application.StartupPath + "\\BackupImages\\backupImages.xml", FileMode.Open));
  419. if(zipXmlModel.picNameList == null)
  420. zipXmlModel.picNameList = new List<PicName>();
  421. //图片名称添加到xml中
  422. PicName onePicName = new PicName();
  423. onePicName.name = nowName;
  424. zipXmlModel.picNameList.Add(onePicName);
  425. string xmlNotes = XmlSerializeHelper.XmlSerialize<ZipXmlModel>(zipXmlModel);
  426. WriteStringToFile(xmlNotes, Application.StartupPath + "\\BackupImages\\backupImages.xml", FileMode.Create);
  427. return true;
  428. }
  429. catch (Exception)
  430. {
  431. return false;
  432. }
  433. }
  434. else
  435. return false;
  436. }
  437. }
  438. /// <summary>
  439. /// 图片备份-内存图片
  440. /// </summary>
  441. /// <param name="bitmap"></param>
  442. /// <param name="bitName"></param>
  443. /// <returns></returns>
  444. public static bool BackupImages(Bitmap bitmap, string bitName, string filePath)
  445. {
  446. //压缩包不做处理
  447. if (!string.IsNullOrEmpty(filePath) && Path.GetExtension(filePath) == ".tga")
  448. return false;
  449. string backupDict = Application.StartupPath + "\\BackupImages";//固定的图片备份文件夹
  450. if (!Directory.Exists(backupDict))
  451. Directory.CreateDirectory(backupDict);
  452. string bitPath = "";
  453. //非本地图片
  454. if (string.IsNullOrEmpty(filePath))
  455. {
  456. bitPath = backupDict + "\\" + bitName + ".jpg";
  457. }
  458. //本地图片
  459. else
  460. {
  461. bitPath = backupDict + "\\" + bitName;
  462. }
  463. if (File.Exists(bitPath))
  464. {
  465. bitPath = GetReNameFile(bitPath);
  466. }
  467. try
  468. {
  469. string extension = Path.GetExtension(bitPath);
  470. switch (extension)
  471. {
  472. case ".jpg":
  473. bitmap.Save(bitPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  474. break;
  475. case ".jpeg":
  476. bitmap.Save(bitPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  477. break;
  478. case ".png":
  479. bitmap.Save(bitPath, System.Drawing.Imaging.ImageFormat.Png);
  480. break;
  481. case ".bmp":
  482. bitmap.Save(bitPath, System.Drawing.Imaging.ImageFormat.Bmp);
  483. break;
  484. case ".gif":
  485. bitmap.Save(bitPath, System.Drawing.Imaging.ImageFormat.Gif);
  486. break;
  487. default:
  488. bitmap.Save(bitPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  489. break;
  490. }
  491. }
  492. catch(Exception)
  493. {
  494. return false;
  495. }
  496. ZipXmlModel zipXmlModel = new ZipXmlModel();
  497. if (System.IO.File.Exists(Application.StartupPath + "\\BackupImages\\backupImages.xml"))
  498. zipXmlModel = XmlSerializeHelper.DESerializer<ZipXmlModel>(ReadStringFromFile(Application.StartupPath + "\\BackupImages\\backupImages.xml", FileMode.Open));
  499. if (zipXmlModel.picNameList == null)
  500. zipXmlModel.picNameList = new List<PicName>();
  501. //图片名称添加到xml中
  502. PicName onePicName = new PicName();
  503. onePicName.name = bitPath;
  504. zipXmlModel.picNameList.Add(onePicName);
  505. string xmlNotes = XmlSerializeHelper.XmlSerialize<ZipXmlModel>(zipXmlModel);
  506. WriteStringToFile(xmlNotes, Application.StartupPath + "\\BackupImages\\backupImages.xml", FileMode.Create);
  507. return true;
  508. }
  509. /// <summary>
  510. /// 恢复图片
  511. /// </summary>
  512. /// <returns></returns>
  513. public static List<string> RestoreImages()
  514. {
  515. string backupDict = Application.StartupPath + "\\BackupImages";//固定的图片备份文件夹
  516. if (!Directory.Exists(backupDict))
  517. return null;
  518. else
  519. {
  520. string backupXml = backupDict + "\\backupImages.xml";
  521. if (System.IO.File.Exists(backupXml))
  522. {
  523. List<string> imageNameList = new List<string>();
  524. ZipXmlModel zipXmlModel = XmlSerializeHelper.DESerializer<ZipXmlModel>(ReadStringFromFile(backupXml, FileMode.Open));
  525. if(zipXmlModel.picNameList != null && zipXmlModel.picNameList.Count > 0)
  526. {
  527. foreach(PicName pic in zipXmlModel.picNameList)
  528. {
  529. imageNameList.Add(pic.name);
  530. }
  531. }
  532. return imageNameList;
  533. }
  534. else
  535. return null;
  536. }
  537. }
  538. /// <summary>
  539. /// 对待恢复的图片做处理
  540. /// </summary>
  541. /// <param name="imgList"></param>
  542. /// <returns></returns>
  543. public static List<string> MoveRestoreImages(List<string> imgList)
  544. {
  545. string oldPath = Application.StartupPath + "\\BackupImages";
  546. string newPath = Application.StartupPath + "\\BackupImagesTemp";
  547. if (Directory.Exists(newPath))
  548. DeleteFolder(newPath);
  549. try
  550. {
  551. Directory.Move(oldPath, newPath);
  552. }
  553. catch (Exception)
  554. {
  555. return null;
  556. }
  557. List<string> newPathList = new List<string>();
  558. foreach (string n in imgList)
  559. {
  560. string oldImg = n.Replace("BackupImages", "BackupImagesTemp");
  561. newPathList.Add(oldImg);
  562. }
  563. return newPathList;
  564. }
  565. public static void RecursionDirector(string dirs, TreeNode node)
  566. {
  567. //绑定到指定的文件夹目录
  568. DirectoryInfo dir = new DirectoryInfo(dirs);
  569. //检索表示当前目录的文件和子目录
  570. FileSystemInfo[] fsinfos = dir.GetFileSystemInfos();
  571. //遍历检索的文件和子目录
  572. foreach (FileSystemInfo fsinfo in fsinfos)
  573. {
  574. //判断是否为空文件夹  
  575. if (fsinfo is DirectoryInfo)
  576. {
  577. TreeNode temp = new TreeNode();
  578. temp.Name = fsinfo.FullName;
  579. temp.Text = fsinfo.FullName;
  580. node.Nodes.Add(temp);
  581. //递归调用
  582. RecursionDirector(fsinfo.FullName, temp);
  583. }
  584. /*else
  585. {
  586. //Console.WriteLine(fsinfo.FullName);
  587. //将得到的文件全路径放入到集合中
  588. //list.Add(fsinfo.FullName);
  589. }*/
  590. }
  591. }
  592. public static string ReadFileOwner(string path)
  593. {
  594. var fs = System.IO.File.GetAccessControl(path);
  595. var sid = fs.GetOwner(typeof(SecurityIdentifier));
  596. var ntAccount = sid.Translate(typeof(NTAccount));
  597. return ntAccount.ToString();
  598. }
  599. /// <summary>
  600. /// 获取相机sn和名称,如果不存在
  601. /// </summary>
  602. /// <param name="path"></param>
  603. /// <param name="cameraSN"></param>
  604. /// <param name="cameraName"></param>
  605. /// <returns></returns>
  606. public static bool CheckCameraSNAndReturnName(string path, string cameraSN, out string cameraName)
  607. {
  608. cameraName = null;
  609. try
  610. {
  611. byte[] buffer = File.ReadAllBytes(path);
  612. string keys = Encoding.UTF8.GetString(buffer);
  613. var bytes = Convert.FromBase64String(keys);
  614. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  615. rsa.FromXmlString(privateKey);
  616. int encriptedChunckSize = rsa.KeySize / 8;
  617. int dataLength = bytes.Length;
  618. int iterations = dataLength / encriptedChunckSize;
  619. ArrayList arrayList = new ArrayList();
  620. for (int i = 0; i < iterations; i++)
  621. {
  622. byte[] tempBytes = new byte[encriptedChunckSize];
  623. System.Buffer.BlockCopy(bytes,
  624. encriptedChunckSize * i,
  625. tempBytes, 0, tempBytes.Length);
  626. System.Array.Reverse(tempBytes);
  627. arrayList.AddRange(rsa.Decrypt(tempBytes, false));
  628. }
  629. var descrypt = (byte[])arrayList.ToArray(typeof(Byte));
  630. string result = Encoding.UTF8.GetString(descrypt);
  631. if (result!=null)
  632. {
  633. string[] arr = result.Split('|');
  634. string v = arr.ToList().Find(a => a.IndexOf(cameraSN) >= 0);
  635. if(!string.IsNullOrEmpty(v))
  636. {
  637. cameraName = v.Split('-')[0];
  638. return true;
  639. }
  640. }
  641. return false;
  642. }
  643. catch
  644. {
  645. }
  646. return false;
  647. }
  648. }
  649. }