I'm having a heck of a time trying to refresh the desktop using a winforms VB.Net application I have written.
The application modifies the registry to un-hide files and folders / protected Operating System files. I have a working method to refresh all Windows Explorer windows after the change to the registry, but the desktop is still left unchanged unless I manually refresh it.
Clicking on the desktop and refreshing using the F5 key on the keyboard successfully refreshes the desktop so that the user can view the hidden icons. But when I use the code below, the desktop flickers, but the hidden files are not displayed unless I physically click on the desktop with the mouse and press F5 on the keyboard:
Private Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Int32, _
ByVal uFlags As Int32, _
ByVal dwitem1 As Int32, _
ByVal deitem2 As Int32) As Int32
Private Sub refreshDesktop()
SHChangeNotify(&H8000000, &H1000, 0, 0)
End Sub
I can't imagine that it should be this difficult. Another alternative I have considered is using SendKeys.Send("{F5}")
but am having trouble bringing focus to the desktop to apply this method to the desktop and not the form. I am testing this on a Windows 7 machine.
Any ideas how to accomplish this / any suggestions or code they know will work is greatly appreaciated as always.
UPDATE: I have put together the SHChangeNotify method signature from Pinvoke.net in-place of what I have above, but it is still returning the same results of the desktop icons flash like the desktop actually IS being refreshed, but not to the point of showing hidden icons, like F5 on the keyboard does. See below for edited code after referenceing Pinvoke.net
<DllImport("Shell32.dll")> _
Shared Sub SHChangeNotify(ByVal wEventID As Int32, _
ByVal uFlags As Int32, _
ByVal dwitem1 As Int32, _
ByVal deitem2 As Int32)
End Sub
Private Sub refreshDesktop()
SHChangeNotify(&H8000000, &H1000, 0, 0)
End Sub