Guest User

Refresh file thumbnail C#

a guest
Sep 3rd, 2010
719
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace Unstub
  8. {
  9.     class ShellNotification
  10.     {
  11.     [DllImport("shell32.dll", CharSet = CharSet.Auto)]
  12.     public static extern Int32 SHParseDisplayName(
  13.         [MarshalAs(UnmanagedType.LPWStr)] String pszName,
  14.         IntPtr pbc,
  15.         out IntPtr ppidl,
  16.         UInt32 sfgaoIn,
  17.         out UInt32 psfgaoOut);
  18.     [DllImport("shell32.dll", CharSet = CharSet.Auto)]
  19.     internal static extern void SHChangeNotify(
  20.         UInt32 wEventId,
  21.         UInt32 uFlags,
  22.         IntPtr dwItem1,
  23.         IntPtr dwItem2);
  24.  
  25.     [Flags]
  26.     private enum ShellChangeNotificationEvents : uint
  27.     {
  28.         //...
  29.         SHCNE_UPDATEITEM = 0x00002000,
  30.         //...
  31.     }
  32.  
  33.     private enum ShellChangeNotificationFlags
  34.     {
  35.         //...
  36.         SHCNF_FLUSH = 0x1000,
  37.         //...
  38.     }
  39.  
  40.     public static void refreshThumbnail(string path)
  41.     {
  42.         try
  43.         {
  44.         uint iAttribute;
  45.         IntPtr pidl;
  46.         SHParseDisplayName(path, IntPtr.Zero, out pidl, 0, out iAttribute);
  47.         SHChangeNotify(
  48.             (uint)ShellChangeNotificationEvents.SHCNE_UPDATEITEM,
  49.             (uint)ShellChangeNotificationFlags.SHCNF_FLUSH,
  50.             pidl,
  51.             IntPtr.Zero);
  52.         }
  53.         catch { }
  54.     }
  55.     }
  56. }
RAW Paste Data