views:

239

answers:

2

Hi,

I am working on a piece of code that removes an extra folder we have in the user's start menu. I start by removing all of the shortcuts it contains, and then remove the folder itself.

After this is done, I can confirm that the shortcuts have been removed from the start menu, but their containing folder remains listed in the start menu. So, I checked the file system for such a folder and found none. Suspecting that this is some sort of refresh problem, I logged my user out and back into Vista and found that the folder was now removed from the start menu list.

How utterly annoying... Does anyone know how to programmatically force a 'refresh' of the Vista start menu, so that the user doesn't see this empty folder before they log out?

Thanks, -Ben

+2  A: 

This article seems to have the answer you're looking for:

http://social.msdn.microsoft.com/forums/en-US/winforms/thread/ce540c7d-a113-4f39-956e-0af6bc91abd3/

The answer given is:

class Program
 {
  [DllImport("user32.dll", SetLastError = true)]
  private static extern IntPtr SendMessageTimeout ( IntPtr hWnd, int Msg, IntPtr wParam, string lParam, uint fuFlags, uint uTimeout, IntPtr lpdwResult );

  private static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);
  private const int WM_SETTINGCHANGE = 0x1a;
  private const int SMTO_ABORTIFHUNG = 0x0002;

  static void Main ( string[] args )
  {
   SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, IntPtr.Zero, null, SMTO_ABORTIFHUNG, 100, IntPtr.Zero);
  }
 }
J.B.
Looks promising, I'll give it a shot. Can't believe my search didn't find that forum post...
Ben
Let me know how it goes. That info might be of use to me later.
J.B.
Unfortunately it didn't seem to work in my situation. I tried calling SendMessageTimeout right after deleting the directory, but it still remains in the start menu. I'm going to try to see if its a timing issue. I'll put it into its own seperate program and run that manually afterwards, and see if that works.
Ben
Rather than running the SendMessageTimout in a separate program, could you do a sleep? Perhaps wait 10 seconds and then let the SendMessageTimeout run?
J.B.
Sorry, got hung up doing other things. I had already partly setup a quick project to run it separately. I ran this after everything else and the entry remained in the start menu, unfortunately.
Ben
+1  A: 

I tried to implement this myself but it did not work as expected using SendMessageTimeout.

Instead, it worked when I used SHGetSpecialFolderLocation(CSIDL_STARTMENU) SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_IDLIST, pidl, NULL);

See this article for sample c++ code: http://support.microsoft.com/kb/q193293/

Tested on Windows Server 2008 Enterprise (x86) with SP1.

Rami A.
I'm so far gone from this problem that I can't verify your solution, but if you claim it works, I'll take your word on it. MS needs to fix these kinds of issues :(
Ben