Skip to content

Instantly share code, notes, and snippets.

@Raymai97
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raymai97/ba94871ba9b7abc810a8 to your computer and use it in GitHub Desktop.
Save Raymai97/ba94871ba9b7abc810a8 to your computer and use it in GitHub Desktop.
Refresh Explorer (apply settings in registry immediately) without restarting Explorer. Most of the time it works, unless you're using bullheaded OS like Windows 8/8.1...
Declare Function SHChangeNotify Lib "Shell32.dll" (ByVal wEventID As Int32, _
ByVal uFlags As Int32, ByVal dwItem1 As Int32, ByVal dwItem2 As Int32) As Int32
Sub RefreshExplorer()
SHChangeNotify(&H8000000, &H0, 0, 0)
Dim CLSID_ShellApplication As New Guid("13709620-C279-11CE-A49E-444553540000")
Dim shellApplicationType As Type = Type.GetTypeFromCLSID(CLSID_ShellApplication, True)
Dim shellApplication As Object = Activator.CreateInstance(shellApplicationType)
Dim windows As Object = shellApplicationType.InvokeMember("Windows", _
System.Reflection.BindingFlags.InvokeMethod, Nothing, shellApplication, New Object() {})
Dim windowsType As Type = windows.[GetType]()
Dim count As Object = windowsType.InvokeMember("Count", _
System.Reflection.BindingFlags.GetProperty, Nothing, windows, Nothing)
If CInt(count) = 0 Then Exit Sub
For i As Integer = 0 To CInt(count) - 1
Dim item As Object = windowsType.InvokeMember("Item", System.Reflection.BindingFlags.InvokeMethod, _
Nothing, windows, New Object() {i})
Dim itemType As Type = item.[GetType]()
Dim itemName As String = DirectCast(itemType.InvokeMember("Name", _
System.Reflection.BindingFlags.GetProperty, Nothing, item, Nothing), String)
If itemName = "Windows Explorer" Or itemName = "File Explorer" Then
itemType.InvokeMember("Refresh", System.Reflection.BindingFlags.InvokeMethod, Nothing, item, Nothing)
End If
Next
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment