- 1 year ago
Update folder icon with desktop.ini & instantly change (C++)
To update a folder icon with desktop.ini
and instantly change it in C++, you can use the following steps:
Create or locate an icon file (
.ico
) that you want to use as the new folder icon.Create a
desktop.ini
file in the target folder or locate an existingdesktop.ini
file.Modify the
desktop.ini
file to specify the icon file and the appropriate settings. Here's an example of thedesktop.ini
file content:ini[.ShellClassInfo]
IconFile=icon.ico
IconIndex=0
ConfirmFileOp=0
Replace
icon.ico
with the name of your icon file and adjust theIconIndex
value if your icon file contains multiple icons.Save the
desktop.ini
file.To make the changes take effect instantly, you can programmatically force an update of the folder's attributes using the
SHChangeNotify
function from the Windows Shell. Here's an example of how to useSHChangeNotify
in C++:cpp#include <windows.h>
#include <shellapi.h>
int main() {
const LPCWSTR folderPath = L"C:\\Path\\to\\Folder";
// Set the folder attributes to trigger an update
DWORD attributes = GetFileAttributesW(folderPath);
SetFileAttributesW(folderPath, attributes | FILE_ATTRIBUTE_HIDDEN);
// Notify the system about the change
SHChangeNotify(SHCNE_ATTRIBUTES, SHCNF_PATHW, folderPath, nullptr);
return 0;
}
Replace
C:\\Path\\to\\Folder
with the actual path to the target folder.This code retrieves the current attributes of the folder using
GetFileAttributesW
, adds theFILE_ATTRIBUTE_HIDDEN
flag to the attributes, and sets the modified attributes usingSetFileAttributesW
. Then, it notifies the system about the change usingSHChangeNotify
with theSHCNE_ATTRIBUTES
event.When the code runs, the folder's attributes will be modified, triggering an update and causing the folder icon to change immediately.
Make sure to compile the code with the necessary libraries and define the appropriate Unicode character set (UNICODE
and _UNICODE
).
Note that changing a folder's icon using desktop.ini
is a Windows-specific feature, and the changes may not be immediately visible on non-Windows systems or in certain file managers that do not support desktop.ini
files.