You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

63 lines
1.8 KiB

3 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace Apewer.Internals
  7. {
  8. internal class NtfsUnlocker
  9. {
  10. #region Win32
  11. [DllImport("kernel32.dll", CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)]
  12. static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr vaListArguments);
  13. [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
  14. static extern int GetFileAttributes(string fileName);
  15. [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
  16. [return: MarshalAs(UnmanagedType.Bool)]
  17. static extern bool DeleteFile(string name);
  18. #endregion
  19. public const string Postfix = ":Zone.Identifier";
  20. /// <summary>获取标记的路径。</summary>
  21. public static string GetZoneIdentifier(string path)
  22. {
  23. if (!File.Exists(path)) return null;
  24. try
  25. {
  26. var info = new FileInfo(path);
  27. var zoneIdentifier = info.FullName + Postfix;
  28. return zoneIdentifier;
  29. }
  30. catch
  31. {
  32. return null;
  33. }
  34. }
  35. /// <summary>检查标记是否存在。</summary>
  36. public static bool ZoneIdentifierExists(string zoneIdentifier)
  37. {
  38. var attributes = GetFileAttributes(zoneIdentifier);
  39. var exists = attributes != -1;
  40. return exists;
  41. }
  42. /// <summary>删除标记。</summary>
  43. public static bool DeleteZoneIdentifier(string zoneIdentifier)
  44. {
  45. var deleted = DeleteFile(zoneIdentifier);
  46. return deleted;
  47. }
  48. }
  49. }