Windows File Protection – How to check whether the file is protected?

15 12 2008


Windows have got a set of files which are essential to the working of system. Chances are lot that those files to be corrupted accidentally or intentionally. But windows have taken care about it and those files are under protection. So if your user selects the filename of a protected system file to save his data, its the duty of a responsible windows citizen  application to check whether its a system file and to warn the user. Well, how to check whether the file is a protected system file?

windowsresourceprotection


You could use the function – SfcIsFileProtected(). Check the code snippet.

#include "Sfc.h"
...
// Check whether the file is protected or not.
WCHAR* pProtectedFile = L"C:\\Windows\\system32\\kernel32.dll";
BOOL bProtected = SfcIsFileProtected( NULL, pProtectedFile );

How to get the list of protected files?
Well, iterating the protected file list will be nice. isn’t it? You can use the function – SfcGetNextProtectedFile() for that. Check the code snippet below.

// Get the protected file list - one by one.
PROTECTED_FILE_DATA ProtectedFileInfo = { 0 };
while( SfcGetNextProtectedFile( NULL, &ProtectedFileInfo ))
{
    // Print the filename.
    cout << ProtectedFileInfo.FileName << endl;
}


Well, for some reason the SfcGetNextProtectedFile() is removed from vista. So keep in mind that it will work only on XP and 2000 machines. Well, don’t forget to include sfc.lib in your project settings. 😉


Targeted Audience – Beginners.

Advertisements