Windows 8.1 – Programmatically refresh the start menu

In the last days I was working on a VDI deployment with Horizon View and AppVolumes. I had the need to be able to programmatically refresh the Start Menu/Start Screen. The problem is that sometimes when an AppStack is mounted the start menu won’t display the shortcuts to the applications in the AppStack and a user can’t launch them form the start menu.

I execute this code via GP and this causes a refresh of the start menu, displaying all new links. Here is the downlodable RefreshStartMenu.exe application, use it at your own risk 😀

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{

	class Program
	{
		private const int SHCNE_UPDATEDIR = 0x00001000;
		private const int SHCNF_IDLIST = 0x0000;
		private const int CSIDL_COMMON_STARTMENU = 0x0016;
		private const int CSIDL_STARTMENU = 0x000b;
		[DllImport("shell32.dll")]
		static extern void SHChangeNotify(int wEventId, int uFlags, IntPtr dwItem1, IntPtr dwItem2);
		[DllImport("shell32.dll")]
		static extern Int32 SHGetFolderLocation(IntPtr hwndOwner, Int32 nFolder, IntPtr hToken, UInt32 dwReserved, out IntPtr ppidl);

		static void Main(string[] args)
		{

			IntPtr pidlCSM;
			IntPtr pidlSM;

			SHGetFolderLocation(IntPtr.Zero, CSIDL_COMMON_STARTMENU, IntPtr.Zero, 0, out pidlCSM);
			SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_IDLIST, pidlCSM, IntPtr.Zero);
			SHGetFolderLocation(IntPtr.Zero, CSIDL_STARTMENU, IntPtr.Zero, 0, out pidlSM);
			SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_IDLIST, pidlSM, IntPtr.Zero);
		}
	}
}