Trouver le répertoire "Démarrer" de Windows avec JNA

Aller en bas

Trouver le répertoire "Démarrer" de Windows avec JNA Empty Trouver le répertoire "Démarrer" de Windows avec JNA

Message  Gus le Lun 10 Déc - 11:20

Le répertoire "Démarrer" de Windows est ce fameux répertoire ou on peut placer des raccourcis des programmes qu'on souhaite démarrer automatiquement au lancement de Windows. Son emplacement dépend du type de Windows mais aussi de la langue. Il n'est pas évident à détecter de manière fiable et aucune variable env disponible par la classe System ne permet a priori de le trouver.

Grâce à l'API JNA (Java Native Access), une API dont j'ai déjà eu souvent l'occasion de vous parler (et ça va continuer), il est possible de trouver ce répertoire, de la manière suivante :

Voici tout d'abord la classe CSIDL qui recense toutes les constantes CSIDL (Constant Special Item ID List) de Windows
(valeurs récupérées à partir du lien : http://www.installmate.com/support/im7/using/symbols/functions/shpath.htm)

Code:
public class CSIDL {

   
   public static final int CSIDL_ADMINTOOLS = 0x30;             //5.0             The file system directory that is used to store administrative tools for an individual user.
   public static final int CSIDL_ALTSTARTUP = 0x1D;            //               The file system directory that corresponds to the user's nonlocalized Startup program group.
   public static final int CSIDL_APPDATA = 0x1A;               //4.71             The file system directory that serves as a common repository for application-specific data.
   public static final int CSIDL_BITBUCKET = 0x0A;               //               The virtual folder containing the objects in the user's Recycle Bin.
   public static final int CSIDL_CDBURN_AREA = 0x3B;            //6.0             The file system directory acting as a staging area for files waiting to be written to CD.
   public static final int CSIDL_COMMON_ADMINTOOLS = 0x2F;       //5.0             The file system directory containing administrative tools for all users of the computer.
   public static final int CSIDL_COMMON_ALTSTARTUP = 0x1E;       //NT-based only    The file system directory that corresponds to the nonlocalized Startup program group for all users.
   public static final int CSIDL_COMMON_APPDATA = 0x23;          //5.0             The file system directory containing application data for all users.
   public static final int CSIDL_COMMON_DESKTOPDIRECTORY = 0x19;    //NT-based only    The file system directory that contains files and folders that appear on the desktop for all users.
   public static final int CSIDL_COMMON_DOCUMENTS = 0x2E;            //               The file system directory that contains documents that are common to all users.
   public static final int CSIDL_COMMON_FAVORITES = 0x1F;          //NT-based only    The file system directory that serves as a common repository for favorite items common to all users.
   public static final int CSIDL_COMMON_MUSIC = 0x35;             //6.0             The file system directory that serves as a repository for music files common to all users.
   public static final int CSIDL_COMMON_PICTURES = 0x36;          //6.0             The file system directory that serves as a repository for image files common to all users.
   public static final int CSIDL_COMMON_PROGRAMS = 0x17;         //NT-based only    The file system directory that contains the directories for the common program groups that appear on the Start menu for all users.
   public static final int CSIDL_COMMON_STARTMENU = 0x16;          //NT-based only    The file system directory that contains the programs and folders that appear on the Start menu for all users.
   public static final int CSIDL_COMMON_STARTUP = 0x18;          //NT-based only    The file system directory that contains the programs that appear in the Startup folder for all users.
   public static final int CSIDL_COMMON_TEMPLATES = 0x2D;          //NT-based only    The file system directory that contains the templates that are available to all users.
   public static final int CSIDL_COMMON_VIDEO = 0x37;             //6.0             The file system directory that serves as a repository for video files common to all users.
   public static final int CSIDL_COMPUTERSNEARME = 0x3D;          //6.0             The folder representing other machines in your workgroup.
   public static final int CSIDL_CONNECTIONS = 0x31;             //6.0             The virtual folder representing Network Connections, containing network and dial-up connections.
   public static final int CSIDL_CONTROLS = 0x03;                //               The virtual folder containing icons for the Control Panel applications.
   public static final int CSIDL_COOKIES = 0x21;                //               The file system directory that serves as a common repository for Internet cookies.
   public static final int CSIDL_DESKTOP = 0x00;                //               The virtual folder representing the Windows desktop, the root of the shell namespace.
   public static final int CSIDL_DESKTOPDIRECTORY = 0x10;          //               The file system directory used to physically store file objects on the desktop.
   public static final int CSIDL_DRIVES = 0x11;                //               The virtual folder representing My Computer, containing everything on the local computer: storage devices, printers, and Control Panel. The folder may also contain mapped network drives.
   public static final int CSIDL_FAVORITES = 0x06;             //               The file system directory that serves as a common repository for the user's favorite items.
   public static final int CSIDL_FONTS = 0x14;                //               A virtual folder containing fonts.
   public static final int CSIDL_HISTORY = 0x22;                //               The file system directory that serves as a common repository for Internet history items.
   public static final int CSIDL_INTERNET = 0x01;                //               A virtual folder for Internet Explorer.
   public static final int CSIDL_INTERNET_CACHE = 0x20;          //4.72             The file system directory that serves as a common repository for temporary Internet files.
   public static final int CSIDL_LOCAL_APPDATA = 0x1C;          //5.0             The file system directory that serves as a data repository for local (nonroaming) applications.
   public static final int CSIDL_MYDOCUMENTS = 0x0C;             //6.0             The virtual folder representing the My Documents desktop item.
   public static final int CSIDL_MYMUSIC = 0x0D;                //6.0             The file system directory that serves as a common repository for music files.
   public static final int CSIDL_MYPICTURES = 0x27;             //5.0             The file system directory that serves as a common repository for image files.
   public static final int CSIDL_MYVIDEO = 0x0E;                //6.0             The file system directory that serves as a common repository for video files.
   public static final int CSIDL_NETHOOD = 0x13;                //               A file system directory containing the link objects that may exist in the My Network Places virtual folder.
   public static final int CSIDL_NETWORK = 0x12;                //               A virtual folder representing Network Neighborhood, the root of the network namespace hierarchy.
   public static final int CSIDL_PERSONAL = 0x05;                //               The file system directory used to physically store a user's common repository of documents. (From shell version 6.0 onwards, CSIDL_PERSONAL is equivalent to CSIDL_MYDOCUMENTS, which is a virtual folder.)
   public static final int CSIDL_PHOTOALBUMS = 0x45;             //Vista          The virtual folder used to store photo albums.
   public static final int CSIDL_PLAYLISTS = 0x3F;             //Vista          The virtual folder used to store play albums.
   public static final int CSIDL_PRINTERS = 0x04;                //               The virtual folder containing installed printers.
   public static final int CSIDL_PRINTHOOD = 0x1B;             //               The file system directory that contains the link objects that can exist in the Printers virtual folder.
   public static final int CSIDL_PROFILE = 0x28;                //5.0             The user's profile folder.
   public static final int CSIDL_PROGRAM_FILES = 0x26;          //5.0             The Program Files folder.
   public static final int CSIDL_PROGRAM_FILES_COMMON = 0x2B;       //5.0             A folder for components that are shared across applications.
   public static final int CSIDL_PROGRAMS = 0x02;                //               The file system directory that contains the user's program groups (which are themselves file system directories).
   public static final int CSIDL_RECENT = 0x08;                //               The file system directory that contains shortcuts to the user's most recently used documents.
   public static final int CSIDL_RESOURCES = 0x38;             //6.0             The file system directory that contains resource data.
   public static final int CSIDL_SAMPLE_MUSIC = 0x40;             //Vista          The file system directory that contains sample music.
   public static final int CSIDL_SAMPLE_PLAYLISTS = 0x41;          //Vista          The file system directory that contains sample playlists.
   public static final int CSIDL_SAMPLE_PICTURES = 0x42;          //Vista          The file system directory that contains sample pictures.
   public static final int CSIDL_SAMPLE_VIDEOS = 0x43;          //Vista          The file system directory that contains sample videos.
   public static final int CSIDL_SENDTO = 0x09;                //               The file system directory that contains Send To menu items.
   public static final int CSIDL_STARTMENU = 0x0B;             //               The file system directory containing Start menu items.
   public static final int CSIDL_STARTUP = 0x07;                //               The file system directory that corresponds to the user's Startup program group.
   public static final int CSIDL_SYSTEM = 0x25;                //5.0             The Windows System folder.
   public static final int CSIDL_TEMPLATES = 0x15;             //               The file system directory that serves as a common repository for document templates.
   public static final int CSIDL_WINDOWS = 0x24;                //5.0             The Windows directory or SYSROOT.
}

Et voici le code qui appelle la méthode SHGetFolderPath à partir de la bibliothèque native Shell32 :

Code:
    private File findStartUpFolder() throws Exception
    {
       if(!Platform.isWindows())
          throw new Exception("Incompatible OS");

       String path = findWinFolderPath(CSIDL.CSIDL_ALTSTARTUP);
       if(path==null) return null;
       return new File(path);
    }
   
   
    private String findWinFolderPath(int nFolder)
    {
       int dwFlags = Shell32.SHGFP_TYPE_CURRENT;
       char[] pszPath = new char[Shell32.MAX_PATH];
       int hResult = Shell32.INSTANCE.SHGetFolderPath(null,nFolder,null,dwFlags,pszPath);
       if(hResult != Shell32.S_OK) return null;

       String path = new String(pszPath);
       int len = path.indexOf('\0');
       return path.substring(0,len);
    }


    private static Map<String,Object> OPTIONS = new HashMap<String,Object>();
   
    static
    {
        OPTIONS.put(Library.OPTION_TYPE_MAPPER, W32APITypeMapper.UNICODE);
        OPTIONS.put(Library.OPTION_FUNCTION_MAPPER,W32APIFunctionMapper.UNICODE);
    }

    static class HANDLE extends PointerType implements NativeMapped {}

    static class HWND extends HANDLE {}

   
   
    static interface Shell32 extends Library
    {
       static Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32",Shell32.class, OPTIONS);

       public static final int MAX_PATH = 260;
        public static final int SHGFP_TYPE_CURRENT = 0;
        public static final int SHGFP_TYPE_DEFAULT = 1;
        public static final int S_OK = 0;
       
        public int SHGetFolderPath(HWND hwndOwner, int nFolder, HANDLE hToken, int dwFlags, char[] pszPath);
    }
Gus
Gus
Admin

Messages : 249
Date d'inscription : 01/09/2009

Voir le profil de l'utilisateur http://www.gus05.com

Revenir en haut Aller en bas

Revenir en haut


 
Permission de ce forum:
Vous ne pouvez pas répondre aux sujets dans ce forum