Need to get Start Menu Paths in Java

2019-07-02 10:16发布

Using java, I would like some code that could get me the paths for: 1) Start Menu for Current User 2) Start Menu for All User

I need the answer for both WinXP and Win7. So hopefully there is a general answer that can get me both.

5条回答
Root(大扎)
2楼-- · 2019-07-02 10:18

Another option is managing Start Menu items from vbs API.

I made a Java Wrapper for that.

// Install Start Menu
WindowsUtils.installStartMenuItem(WindowsUtils.SPECIALFOLDER_Programs,"my_start_menu", "explorer.exe", "http://www.google.es","Acceso directo a google");

// Uninstall Start Menu
WindowsUtils.uninstallStartMenuItem(WindowsUtils.SPECIALFOLDER_Programs, "my_start_menu");
查看更多
SAY GOODBYE
3楼-- · 2019-07-02 10:36

Okay, I figured out a solution, but maybe someone else has a more eligant one.

I plan on doing something like "Runtime.getRuntime().exec(command);" and the command will be a "reg query" to query the following registry keys:

Current User can referenced by: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Start Menu

All users can be referenced by: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Common Start Menu

These are the same for both Win7 and WinXP. If anyone else knows of a better solution, I'll be happy to look at it too.

查看更多
你好瞎i
4楼-- · 2019-07-02 10:40

You have no other choice but to write a DLL and call native Windows API:

  • SHGetFolderPath(NULL, CSIDL_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, &szPathBuffer)
  • SHGetFolderPath(NULL, CSIDL_COMMON_PROGRAMS, NULL, SHGFP_TYPE_CURRENT, &szPathBuffer)

If you really need the root of Start menu, use CSIDL_STARTMENU and CSIDL_COMMON_STARTMENU.
The full list of known folders: CSIDL.

If you target Windows Vista and above, use SHGetKnownFolderPath function instead of SHGetFolderPath.

You can use JNA library to call native Windows API without writing native code yourself but pure Java code.

查看更多
来,给爷笑一个
5楼-- · 2019-07-02 10:42

i recently found this

public class VBSUtils {

  public static String SF_ALLUSERSDESKTOP    = "AllUsersDesktop";
  public static String SF_ALLUSERSSTARTMENU  = "AllUsersStartMenu";
  public static String SF_ALLUSERSPROGRAMS   = "AllUsersPrograms";
  public static String SF_ALLUSERSSTARTUP    = "AllUsersStartup";
  public static String SF_DESKTOP            = "Desktop";
  public static String SF_FAVORITES          = "Favorites";
  public static String SF_MYDOCUMENT         = "MyDocuments";
  public static String SF_PROGRAMS           = "Programs";
  public static String SF_RECENT             = "Recent";
  public static String SF_SENDTO             = "SendTo";
  public static String SF_STARTMENU          = "StartMenu";

  private VBSUtils() {  }

  public static String getSpecialFolder(String folder) {
    String result = "";
    try {
        File file = File.createTempFile("realhowto",".vbs");
        file.deleteOnExit();
        FileWriter fw = new java.io.FileWriter(file);

        String vbs = "Set WshShell = WScript.CreateObject(\"WScript.Shell\")\n"
                     + "wscript.echo WshShell.SpecialFolders(\"" + folder + "\")\n"
                     + "Set WSHShell = Nothing\n";

        fw.write(vbs);
        fw.close();
        Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
        BufferedReader input =
            new BufferedReader
              (new InputStreamReader(p.getInputStream()));
        result = input.readLine();
        input.close();
    }
    catch(Exception e){
        e.printStackTrace();
    }
    return result;
  }

      public static void main(String[] args){
          System.out.println(VBSUtils.getSpecialFolder(VBSUtils.SF_ALLUSERSSTARTMENU));
        System.out.println(VBSUtils.getSpecialFolder(VBSUtils.SF_ALLUSERSDESKTOP));
        System.out.println(VBSUtils.getSpecialFolder(VBSUtils.SF_DESKTOP));
        System.out.println(VBSUtils.getSpecialFolder(VBSUtils.SF_PROGRAMS));
        //System.out.println(VBSUtils.getSpecialFolder(VBSUtils.SF_STARTUP));
      }
    }
查看更多
手持菜刀,她持情操
6楼-- · 2019-07-02 10:45

In my program I used a simple System.getProperty("user.home") + "/Start Menu/Programs" This gave me the user's Start Menu folder.

It worked on windows 7 and windows 10. I tried this because in order to get a user's desktop, all I had to do was call System.getProperty("user.home") + "/Desktop". SO I figured that it might work for the Start Menu as well, and seemed to have worked fine. I can delete and write files to the Start Menu just like I can with the desktop. Whether this is the right way to do something like this or not, I have no idea. But I'm just sharing what worked for me.

查看更多
登录 后发表回答