簡體   English   中英

使用 desktop.ini 更新文件夾圖標並立即更改(C++)

[英]Update folder icon with desktop.ini & instantly change (C++)

在閱讀了很多內容並連續嘗試了 2 天之后,我設法按照 Microsoft 關於文件夾圖標的教程完成了一些工作,但這並不完美 - 更改不會立即發生。 我需要重新啟動資源管理器/重新啟動計算機或等待幾分鍾(隨機),直到我看到舊圖標變為新圖標。

TL;博士

預期結果:我想以編程方式更新我的文件夾圖標,並看到新圖標立即出現,無需等待。 (使用c++.batnode.js或其他任何可行的方法,我真的不介意)。

問題:鏈接我的.ico文件后,我希望立即看到結果,但我只能在 30 秒到 4 分鍾左右后看到它。 它有時也會立即發生變化,但很少發生,我希望它 100% 的時間都能正常工作。

要求和注意事項:

  • 不能重新啟動資源管理器。
  • 如果它不會從所有文件夾和文件中清除整個縮略圖緩存會更好,因為它只需要更新單個文件夾圖標。

希望/失敗的嘗試

以編程方式重命名desktop.ini (不幸的是,無法正常工作)

我找到了一種立即查看圖標更改的方法, 但它僅在通過資源管理器本身手動重命名 desktop.ini 時才有效 (將 desktop.ini 更改為大寫/小寫或 desktop-temp.ini,然后返回 desktop.ini)這樣做時,我看到文件夾圖標立即更改。

這讓我希望我可以以編程方式做同樣的事情,所以我嘗試了這個 C++ 代碼,將desktop.ini更改為desktop-temp.ini並返回到desktop.ini ,同時通知 windows 發生了變化,但是不幸的是,它沒有用。

#include <stdio.h>
#include <Windows.h>
#include <winbase.h>
#include <Tchar.h>
#include <shlobj.h> // for SHGetSpecialFolderPathA


const char folderpath[] = "C:\\Users\\elron\\Elron Apps C\\005 Folder Icon\\paste-folder-icon";

int main()
{
    // rename("C:\\Users\\elron\\Elron Apps C\\005 Folder Icon\\paste-folder-icon\\desktop.ini", "desktop-temp.ini");

    if (MoveFile(_T("C:\\Users\\elron\\Elron Apps C\\005 Folder Icon\\paste-folder-icon\\desktop.ini"), _T("desktop-temp.ini")))
    {
        printf("succeeded\n");
    }
    else
    {
        printf("Error %d\n", GetLastError());
    }

    SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_NOTIFYRECURSIVE, NULL, NULL);
    SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_NOTIFYRECURSIVE, folderpath, NULL);

    if (MoveFile(_T("C:\\Users\\elron\\Elron Apps C\\005 Folder Icon\\paste-folder-icon\\desktop-temp.ini"), _T("desktop.ini")))
    {
        printf("succeeded\n");
    }
    else
    {
        printf("Error %d\n", GetLastError());
    }

    SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_NOTIFYRECURSIVE, NULL, NULL);
    SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_NOTIFYRECURSIVE, folderpath, NULL);
}

SHChangeNotify() (有效,但不是立即)

靈感來源: 如何在 Windows 中立即刷新文件夾圖標

I tried running this C++ code (after compiling using npm command g++ -o refresh-folder-icons refresh-folder-icons.cpp ) and it worked, but I the changes take place between 1 to 5 minutes and if you're lucky. 然后你立即看到它。 這是不可靠的。 我在兩台計算機(均為 Windows 10)上對此進行了測試。

#include <windows.h>
#include <ShlObj.h>

const char folderPath[] = "path/to/folder";

int main()
{
    SHChangeNotify(SHCNE_UPDATEITEM, SHCNF_PATH, folderPath, NULL);
    SHChangeNotify(SHCNE_ALLEVENTS, SHCNF_NOTIFYRECURSIVE, "path/to/folder/desktop.ini", NULL);
}

"Use a shell function that will notify all running Explorer windows to use the updated desktop.ini" using VBScript (Did not work for me)

我還嘗試創建一個.bat文件,然后是: 更改 desktop.ini 不會在 Windows 中自動更新文件夾圖標,但我的文件夾仍然顯示以前的圖標。

使用 IE4UINIT 刷新圖標

按照此

運行以下命令(對於 Windows 10)並按 Enter: ie4uinit.exe -show對於 Windows 8 及更早版本,請運行以下命令: ie4uinit.exe -ClearIconCache

使用NirCmd刷新圖標

按照本教程(第 3 步) ,我嘗試安裝NirCmd並運行所有三個,它們都沒有刷新我的文件夾圖標:

nircmd.exe sysrefresh 

nircmd.exe sysrefresh environment 

nircmd.exe sysrefresh policy

SHGetSetFolderCustomSettings() (我的主要希望!!!)

有人在這里寫道SHGetSetFolderCustomSettings “函數設置一個圖標並自行發送所有必要的通知”,這真的讓我希望這是正確的道路。

此外,在 Peter Tseng 的回答herehere中,他展示了一個有效的示例代碼,但是當我嘗試將此 C++ 代碼編譯成.exe時,我得到了錯誤。 這是代碼:

#include <windows.h>
#include <shlobj.h>

int main()
{
    SHFOLDERCUSTOMSETTINGS pfcs;
    pfcs.dwMask = "C:\\Users\\elron\\Elron Apps C\\005 Folder Icon\\paste-folder-icon\\folderico-1629960936.ico";
    pfcs.pszIconFile = "C:\\Users\\elron\\Elron Apps C\\005 Folder Icon\\paste-folder-icon\\folderico-1629960936.ico";
    pfcs.cchIconFile = 0;
    pfcs.iIconIndex = 0;
    PCWSTR pszPath = L"C:\\Users\\elron\\Elron Apps C\\005 Folder Icon\\paste-folder-icon\\test\\hey";
    SHGetSetFolderCustomSettings(pfcs, pszPath, FCS_FORCEWRITE);
}

但我得到三個錯誤,都表明同一個問題:

  • 在此 scope (gcc) 中未聲明'SHFOLDERCUSTOMSETTINGS'
  • 在此 scope (gcc) 中未聲明'pfcs'
  • 'SHGetSetFolderCustomSettings'未在此 scope (gcc) 中聲明

為什么我會收到此錯誤? 我缺少依賴項嗎? 我正在使用 windows 10。


我設法開始使用最后一種方法(使用SHGetSetFolderCustomSettings()

#include <windows.h>
#include <shlobj.h>

int main()
{
    /*
    convert the icon's path to LPWSTR, as required by field pszIconFile
    method taken from https://stackoverflow.com/questions/29847036/convert-char-to-wchar-t-using-mbstowcs-s
    */
    const char* iconPath = "C:\\Users\\YourUsername\\PathTo\\YourIcon\\file.ico";
    size_t size = strlen(iconPath) + 1;
    wchar_t* wtext = new wchar_t[size];

    size_t outSize;
    mbstowcs_s(&outSize, wtext, size, iconPath, size - 1);
    LPWSTR ptr = wtext;

    // (fixed the initialization of some fields)
    SHFOLDERCUSTOMSETTINGS pfcs{};
    pfcs.dwSize = sizeof(SHFOLDERCUSTOMSETTINGS);
    pfcs.dwMask = FCSM_ICONFILE;
    pfcs.pszIconFile = ptr;
    pfcs.cchIconFile = 0;
    pfcs.iIconIndex = 0;
    PCWSTR pszPath = L"C:\\Users\\YourUsername\\PathTo\\YourFolder\\FolderToCustomize";
    SHGetSetFolderCustomSettings(&pfcs, pszPath, FCS_FORCEWRITE);
}

我認為你提到的問題:

但我得到三個錯誤,都表明同一個問題:

  • 在此 scope (gcc) 中未聲明'SHFOLDERCUSTOMSETTINGS'

可能是因為您沒有使用適當的編譯器。 直接從控制台/Visual Studio Code 上使用g++時,我遇到了類似的編譯錯誤,但是在使用 Visual Studio 2022(Visual C++ 編譯器)時,相同的代碼可以工作。 我也是 C++ 的新手,所以如果這不是問題的根源,請隨時糾正我。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM