I'm creating a new shorcut within and update of my program on the Start Menu I worked getting the Special Environment variable ALLUSERSPROFILE and it worked for me under XP, it returns the right path, when using it under vista ir returns c:\ProgramData which is useless. Reading the Environment variable StartMenu is also pointless it returns empty string. ( On vista it lies under Windows\Start Menu, in english ,and if the install folder Windows has the default name) Does anyone has an Idea how to get the startmenu directory for the 'All Users". and would it be a generic solution that works under XP and Vista?
+2
A:
You want CSIDL_COMMON_STARTMENU
. This doesn't appear to be defined in the Environment.SpecialFolders
enumeration, but you can use the Win32 API via P/Invoke:
[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,
[Out] StringBuilder lpszPath, int nFolder, bool fCreate);
int CSIDL_COMMON_STARTMENU = 0x16;
StringBuilder path = new StringBuilder(260);
SHGetSpecialFolderPath(IntPtr.Zero, path, CSIDL_COMMON_STARTMENU, false);
CSIDL_COMMON_STARTMENU (FOLDERID_CommonStartMenu) The file system directory that contains the programs and folders that appear on the Start menu for all users. A typical path is C:\Documents and Settings\All Users\Start Menu. Valid only for Windows NT systems.
Michael Petrotta
2010-03-04 17:31:03
IS this is only valid for NT Systems?
jmayor
2010-03-04 17:32:12
@jmayor: Yes. Are you concerned about Windows 98?
SLaks
2010-03-04 17:33:00
@jmayor: "Windows NT" is usually used in MSDN to refer to "NT-lineage" operating systems (NT, 2k, XP, Vista, 7), as opposed to "classic" Windows (3.1, 95, 98, Me).
Michael Petrotta
2010-03-04 17:34:47
I like it, very neat solution, Btw do you know how does it works in the back end, does it looks the data over the registry?. Thanks a lot
jmayor
2010-03-04 18:13:47
You're welcome. A lot of the Win32 APIs you'll see will, in the end, look up data in the registry. It's safer to use the programmatic calls, though, because registry locations can and do change from one Windows release to another. Not always, but sometimes. The `SHGetSpecialFolderPath` function is documented, supported, and will **always** return the correct path on a given system.
Michael Petrotta
2010-03-04 18:16:58
No, the folder paths in registry are there to support 3 programs written for Windows 95 Beta to continue running on Windows 95 RTM. New code should not get the paths from the registry.
Sheng Jiang 蒋晟
2010-03-04 20:05:26
@Sheng Jiang Where from?
jmayor
2010-03-04 20:47:07
@jmayor: use the call I describe in my answer, rather than using the registry.
Michael Petrotta
2010-03-04 22:35:45
A:
Looking under the registry I found the following key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders there is a value named Common Start Menu that contains the path. It works on vista and XP
jmayor
2010-03-04 17:38:10
Do not read it off the registry. Use documented APIs instead. These values aren't created until certain Shell APIs are called. You will discover this if you start from a fresh install of Windows and add system restore to your test process.
Sheng Jiang 蒋晟
2010-03-04 20:08:48