Force the icons on the desktop to refresh after de

2019-05-05 14:24发布

问题:

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?

回答1:

ou can use the SHChangeNotify from Shell32.dll

You've got a function in PowerShell.com



回答2:

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