Refresh Explorer


posted by Tobias
11-16-2010

Downloads: 862
File size: 1.1kB
Views: 5,485

Embed
Refresh Explorer
  1. function Refresh-Explorer
  2.     $code = @' 
  3. private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);  
  4. private const int WM_SETTINGCHANGE = 0x1a;  
  5. private const int SMTO_ABORTIFHUNG = 0x0002;  
  6.  
  7.  
  8. [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)] 
  9. static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, 
  10.    IntPtr lParam); 
  11.  
  12. [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]  
  13.   private static extern IntPtr SendMessageTimeout ( IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult );  
  14.  
  15.  
  16. [System.Runtime.InteropServices.DllImport("Shell32.dll")]  
  17. private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2); 
  18.  
  19.  
  20. public static void Refresh()  { 
  21.     SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero); 
  22.     SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);  
  23. } 
  24. '
  25.  
  26.     Add-Type -MemberDefinition $code -Namespace MyWinAPI -Name Explorer  
  27.     [MyWinAPI.Explorer]::Refresh() 
creates a function called Refresh-Explorer which calls native API functions to make explorer refresh its settings. So whenever you have changed registry values or other configuration details and want to inform explorer of these, use this function which avoids a reboot.
Copyright 2012 PowerShell.com. All rights reserved.