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.

53 lines
1.6 KiB

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