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.

55 lines
1.6 KiB

3 years ago
2 years ago
3 years ago
2 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. public static bool FileExists(string path) => GetFileAttributes(path) != -1;
  21. public static string DeleteZoneIdentifier(string path)
  22. {
  23. if (!FileExists(path)) return "文件不存在。";
  24. try
  25. {
  26. var info = new FileInfo(path);
  27. var streamPath = info.FullName + Postfix;
  28. var streamExists = FileExists(streamPath);
  29. if (!streamExists) return null;
  30. var deleted = DeleteFile(streamPath);
  31. if (!deleted) return $"未能删除 {streamPath}。";
  32. return null;
  33. }
  34. catch (Exception ex)
  35. {
  36. return ex.Message;
  37. }
  38. }
  39. }
  40. }