Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open folder picker dialog in vscode?

I am writing extension that has to support creating new, custom project templates (directory structure and few files) in a folder chosen by user. Is there any way to open folder picker dialog in vscode?

like image 520
rkrahl Avatar asked Aug 04 '17 07:08

rkrahl


2 Answers

File dialogs were added in VSCode 1.17. See window.showOpenDialog and window.showSaveDialog.

They don't appear to choose a folder without a file, but they do allow multi-select and of course you can just take the path name of any chosen file.

const options: vscode.OpenDialogOptions = {
     canSelectMany: false,
     openLabel: 'Open',
     filters: {
        'Text files': ['txt'],
        'All files': ['*']
    }
};

vscode.window.showOpenDialog(options).then(fileUri => {
    if (fileUri && fileUri[0]) {
        console.log('Selected file: ' + fileUri[0].fsPath);
    }
});

Note you may need to update your package.json file to get access to this new API.

"engines": {
    "vscode": "^1.17.0"
},
like image 85
emackey Avatar answered Sep 21 '22 06:09

emackey


Now we can select folder using window.showOpenDialog. Simply adjust options according to your need.

    const options: vscode.OpenDialogOptions = {
        canSelectMany: false,
        openLabel: 'Select',
        canSelectFiles: false,
        canSelectFolders: true
    };
   
   vscode.window.showOpenDialog(options).then(fileUri => {
       if (fileUri && fileUri[0]) {
           console.log('Selected file: ' + fileUri[0].fsPath);
       }
   });

Currently I'm working on Vs Code version: 1.51.0

like image 43
Nouman Ahmad Avatar answered Sep 21 '22 06:09

Nouman Ahmad