Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force the icons on the desktop to refresh after deleting items, or stop an item from being added in the first place

I have created a powershell script that listens for files to be created on the desktop. The file is immediately deleted if it meets certain criteria. I used Remove-Item $path where $path is the path to the file I want to delete. The problem is that windows still adds, and continues to show the item on the desktop. The file is definitely not there, since attempting to manipulate it will result in a 'Could not find this item', or 'File does not exist' error. Manually refreshing the desktop via 'Right Click => Refresh' will cause the item to be removed.

Is there a way to force the desktop to refresh after deleting an item on it? Otherwise, is there an alternate method to delete the file to prevent it being added in the first place?

like image 882
fishpen0 Avatar asked Apr 03 '12 03:04

fishpen0


People also ask

How do I refresh my desktop icons?

Refresh Icon Cache in Windows If your icon cache is corrupted or Windows is not able to create the new image of the replaced icon, you need to rebuild or refresh the icon cache. To refresh the icon cache, just delete the iconCache. db file and Windows will automatically start rebuilding the new cache.

How do I remove an icon from my desktop that won't delete?

You must right-click an empty space on the desktop. On the right side of the Personalization settings window, click the Themes option. Scroll down to the Related settings section and click the Desktop icon settings option. Uncheck the box next to the desktop icon(s) you want to remove, click Apply, and click OK.


2 Answers

For anyone still looking for an answer I'll repost my answer to this question here as well, as the links to PowerShel.com seem not to work anymore:

I used the following to call a refresh on the desktop from powershell by using C# code:

  $code = @'
  [System.Runtime.InteropServices.DllImport("Shell32.dll")] 
  private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2);

  public static void Refresh()  {
      SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);    
  }
'@

Add-Type -MemberDefinition $code -Namespace WinAPI -Name Explorer 
[WinAPI.Explorer]::Refresh()

Hope this helps anyone still looking for an answer.

p.s. this is where I got the idea from IDERA - Refreshing Icon Cache

like image 80
memory of a dream Avatar answered Sep 21 '22 06:09

memory of a dream


ou can use the SHChangeNotify from Shell32.dll

You've got a function in former PowerShell.com, no longer available

function Refresh-Explorer { 
    $code = @' 
private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);  
private const int WM_SETTINGCHANGE = 0x1a;  
private const int SMTO_ABORTIFHUNG = 0x0002;  
 
 
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)] 
static extern bool SendNotifyMessage(IntPtr hWnd, uint Msg, UIntPtr wParam, 
   IntPtr lParam); 
 
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]  
  private static extern IntPtr SendMessageTimeout ( IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult );  
 
 
[System.Runtime.InteropServices.DllImport("Shell32.dll")]  
private static extern int SHChangeNotify(int eventId, int flags, IntPtr item1, IntPtr item2); 
 
 
public static void Refresh()  { 
    SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero); 
    SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);  
} 
'@ 
 
    Add-Type -MemberDefinition $code -Namespace MyWinAPI -Name Explorer  
    [MyWinAPI.Explorer]::Refresh() 
}
like image 37
JPBlanc Avatar answered Sep 24 '22 06:09

JPBlanc