[英]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?有没有办法在 vscode 中打开文件夹选择器对话框?
File dialogs were added in VSCode 1.17. VSCode 1.17 中添加了文件对话框。 See window.showOpenDialog
and window.showSaveDialog
.请参阅window.showOpenDialog
和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.请注意,您可能需要更新package.json
文件才能访问此新 API。
"engines": {
"vscode": "^1.17.0"
},
Now we can select folder using window.showOpenDialog.现在我们可以使用 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目前我正在研究 Vs Code 版本:1.51.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.