Search code examples
nsis

How to show only local drives and folders from the Browse dialog when installing using NSIS?


Once opened the browse dialog when installing using NSIS, it is showing the list of drivers, folders, mapped drives, removable drives and network folders. How to filter it and show only the local drives and folders from the Browse dialog?


Solution

  • You cannot change the way the directory page works, you would have to write a custom page and a custom plug-in if you want to filter the folder dialog.

    You can however validate the directory and block the user from moving to the next page:

    !include LogicLib.nsh
    Page Directory
    Page InstFiles
    
    !define /IfNDef DRIVE_FIXED 3
    Function .onVerifyInstDir
    StrCpy $0 $InstDir 1 
    System::Call 'KERNEL32::GetDriveType(t"$0:\")i.r0'
    ${If} $0 <> ${DRIVE_FIXED}
        Abort
    ${EndIf}
    FunctionEnd
    

    In this specific case that might not be a good idea because the user has no idea why they can't click the next/install button.

    Instead you should stop with a message when the user tries to leave the page:

    !include LogicLib.nsh
    Page Directory "" "" ValidateDirPage
    Page InstFiles
    
    !define /IfNDef DRIVE_FIXED 3
    Function ValidateDirPage
    StrCpy $0 $InstDir 1 
    System::Call 'KERNEL32::GetDriveType(t"$0:\")i.r0'
    ${If} $0 <> ${DRIVE_FIXED}
        MessageBox MB_ICONSTOP "You must specify a local fixed drive for some reason!"
        Abort
    ${EndIf}
    FunctionEnd
    

    Note: Some USB storage devices will identify as a fixed drive.

    Even if you do all this you cannot stop people from installing to a different drive type. They could temporarily change their drive letters, install, and then change them back etc.