Windows File Protection (SFC) 防止關鍵系統檔案被覆寫。開發安裝程式、修復工具時常需查詢某檔案是否受保護、或列舉所有受保護檔案。
SfcGetNextProtectedFile:迭代列舉。SfcIsFileProtected:檢測單一檔案。
#include
// 需專案加入 sfc.lib
// 列舉所有受保護檔案
void __fastcall ListAllProtectedFile(TStrings *pList)
{
PROTECTED_FILE_DATA data = {0};
while (SfcGetNextProtectedFile(NULL, &data)) {
if (data.FileNumber != 0)
pList->Add(data.FileName);
}
}
// 檢測單一檔案
bool __fastcall IsFileProtected(AnsiString strFile)
{
WCHAR wszFileName[MAX_PATH];
MultiByteToWideChar(CP_ACP, 0, strFile.c_str(), -1, wszFileName, MAX_PATH);
return SfcIsFileProtected(NULL, wszFileName);
}
// 使用範例
if (IsFileProtected("E:\\Winnt\\system32\\subst1.exe"))
ShowMessage("受保護");
else
ShowMessage("未受保護");