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

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 bool FileExists(string path) => GetFileAttributes(path) != -1;
public static string DeleteZoneIdentifier(string path)
{
if (!FileExists(path)) return "文件不存在。";
try
{
var info = new FileInfo(path);
var streamPath = info.FullName + Postfix;
var streamExists = FileExists(streamPath);
if (!streamExists) return null;
var deleted = DeleteFile(streamPath);
if (!deleted) return $"未能删除 {streamPath}。";
return null;
}
catch (Exception ex)
{
return ex.Message;
}
}
}
}