开发者

Reaching Start Menu Directory on Windows Vista and 7

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 vist开发者_JAVA技巧a 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?


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.


There is no Win32 API needed and no manually registry access. I am using Microsoft Framework 4.0 and can do the following successfully under Win7 and XP:

 Dim diStartMenu As New IO.DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu))
 fiShortCut = New IO.FileInfo(IO.Path.Combine(diStartMenu.FullName, ApplicationTitle & ".lnk"))

this will return the following path for diStartMenu:

C:\ProgramData\Microsoft\Windows\Start Menu

this corresponse fully with the registry value in

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜