简体   繁体   English

如何在 vscode 中打开文件夹选择器对话框?

[英]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.showOpenDialogwindow.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.

相关问题 如何使用预先选择的文件在 vscode 中打开文件夹选择器对话框? - How to open folder picker dialog in vscode with a pre-selected file? 使用简单文件对话框时,是否可以在 VSCode 中获得组合的打开文件夹/打开文件对话框? - Is it possible to get a combined open folder/open file dialog in VSCode when using the simple file dialog? 如何在同一实例中在 VSCode 中打开另一个文件夹? - How to open another folder in VSCode in the same instance? 如何通过CMD打开整个文件夹到VSCode? - How to open an entire folder through CMD to VSCode? VSCode 远程容器 — 如何打开 VSCode 的容器实例中的默认文件夹? - VSCode Remote Container — how to open the default folder in the container instance of VSCode? VScode(突然)不会打开文件夹 - VScode(suddenly) will not open a folder 如何从“转到文件”对话框中获取VSCode以“作为新文件打开” - How to get VSCode to “open as new file” from Go To File dialog VSCode打开工作区文件夹热键 - VSCode open workspace folder hotkey VSCode 未在打开的文件夹上运行任务 - VSCode not running task on folder open 如何在 VSCode 中打开文件夹而不记住上一个会话中打开的文件编辑器? - How to open a folder in VSCode without it remembering the open file editors from previous session?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM