14

Yeah, I know this seems like a dumb question, its just a one-off hack I need to wrap up a somewhat mundane task so I can move on to something more interesting.

EDIT: Maybe more info would help: I'm trying to remove some shortcuts from the desktop and I need the user to see it removed right away (so they don't have to press F5).

dso
  • 9,463
  • 10
  • 53
  • 59

2 Answers2

40

You can use the SHChangeNotify API.

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

and then call it this way

SHChangeNotify(0x8000000, 0x1000, IntPtr.Zero, IntPtr.Zero);
Dan Dar3
  • 1,199
  • 13
  • 23
Tom Anderson
  • 10,807
  • 3
  • 46
  • 63
  • 1
    @DSO How did you "Cut" it? – SepehrM Jul 31 '14 at 08:48
  • 2
    Any idea what the reason is behind having to pass `SHCNE_ASSOCCHANGED (0x8000000)` as `eventId`? – Steven Jeuris Jan 29 '15 at 17:19
  • I'm using Squirrel installer and removing two icons. What I'm seeing using this method is that it's unreliable - sometimes works, sometimes not. I've tried adding in a delay (1.5s) and that helps but it still fails. – imekon Sep 27 '21 at 09:51
3

I think you're looking for IActiveDesktop::ApplyChanges. You will need to access this via the COM interface, which should be fairly easy with all the documentation Microsoft provides on COM Interop.

John T
  • 23,735
  • 11
  • 56
  • 82
  • Thanks for the tip, looks like that this would probably work... however the Win32 approach from Tom was a bit easier to use right away. – dso Mar 15 '09 at 05:00