Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

winapi - How to refresh Windows Explorer

enter image description here

A network volume's name can be changed.
I changed the name to Spinal Disk
enter image description here

enter image description here

To change a network name, I do

  • Write new name in Registry.
  • Attach my network volume using functions like DefineDosDeviceW. In this timing, Windows explorer read registry in my guessing.
  • SHChangeNotify(SHCNE_DRIVEADD, SHCNF_PATH, root, NULL); // To be sure.
  • SendMessageTimeout(HWND_BROADCAST, WM_DEVICECHANGE, message, (LPARAM)(&dbv), - SMTO_ABORTIFHUNG, 200, &dwResult); // To be sure again!

It works, but sometimes doesn't work even though I use SHChangeNotify and WM_DEVICECHANGE to be sure. Sometimes, the new name is not applied in Windows Explorer.
But if I terminate Explorer and re-execute, the name applied.

I'm finding an interface having Windows Explorer refreshed.(Is it correct syntax? Sorry, I can't express this sentence well.)
Is there a proper function? Explorer should re-read registry and be refreshed.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately, Explorer simply can't refresh every time it is told so. Because way too many apps behave badly and always use SHChangeNotify() with SHCNE_ALLEVENTS. Imagine if the explorer would really always refreshed everything every time it receives that notification (yes, sometimes several times per second!). That's why it sometimes doesn't work.

But here's how you can 'trick' the explorer:
Send a notification for more than one path, from the bottom up. Usually it's enough to first send a notification for e.g. N:folder and then for N: to really refresh N:.

And you should send the WM_DEVICECHANGE message first, and only then call SHChangeNotify().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...