command:revealFileInOS
是打开文件所在的文件夹,而不是打开文件。
我使用了错误的命令吗?
我也尝试了命令
vscode.open
,但它打开了vscode
内部的文件。
我不是想在
vscode
中打开文件,我只是想像双击/打开它一样打开它。
const uri = vscode.Uri.file(item.originalImagePath);
const args = [uri];
const openFileCommandUrl = vscode.Uri.parse(
`command:revealInExplorer?${encodeURIComponent(JSON.stringify(args))}`);
const browseFileCommandUrl = vscode.Uri.parse(
`command:revealFileInOS?${encodeURIComponent(JSON.stringify(args))}`);
我认为没有内置方法可以在 vscode 中的操作系统默认应用程序中打开该文件。请参阅待办事项中的添加使用操作系统的默认应用程序打开文件的功能。
但是这个包看起来很有前途:open,那是一个 npm 链接。
npm install open
在您的扩展文件夹中。执行此操作后,请确保您可以在 package.json
devDependencies
中看到它。
const open = require("open");
或您想要在要使用它的文件中命名的任何名称。
await open(<yourFullPathFileNameHere>)
所以你的 item.originalImagePath
应该可以在那里工作。
以下是我如何测试它以让当前编辑器(包含图像)在默认应用程序中打开该图像:
const filePath = vscode.window.tabGroups.activeTabGroup.activeTab.input.uri.fsPath;
await open(filePath); // using whatever you named it in the require
它对我来说在 W11 上效果很好。有几个选项可与
open
命令一起使用。
这似乎有效。 请阅读为什么您需要 Uri.file 而不是 Uri.parse: ELIO STRUYF 博客文章
// do not use vscode.Uri.parse
const yourUri = vscode.Uri.file(yourPath);
vscode.commands.executeCommand("revealFileInOS", yourUri);