using System; using System.Collections.Generic; using System.IO; using System.Runtime.InteropServices; using System.Text; namespace Apewer.Internals { internal class NtfsUnlocker { #region Win32 [DllImport("kernel32.dll", CharSet = CharSet.Auto, BestFitMapping = false, ThrowOnUnmappableChar = true)] static extern int FormatMessage(int dwFlags, IntPtr lpSource, int dwMessageId, int dwLanguageId, StringBuilder lpBuffer, int nSize, IntPtr vaListArguments); [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] static extern int GetFileAttributes(string fileName); [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool DeleteFile(string name); #endregion public const string Postfix = ":Zone.Identifier"; /// 获取标记的路径。 public static string GetZoneIdentifier(string path) { if (!File.Exists(path)) return null; try { var info = new FileInfo(path); var zoneIdentifier = info.FullName + Postfix; return zoneIdentifier; } catch { return null; } } /// 检查标记是否存在。 public static bool ZoneIdentifierExists(string zoneIdentifier) { var attributes = GetFileAttributes(zoneIdentifier); var exists = attributes != -1; return exists; } /// 删除标记。 public static bool DeleteZoneIdentifier(string zoneIdentifier) { var deleted = DeleteFile(zoneIdentifier); return deleted; } } }