VS Code 扩展 - 获取完整路径 [英] VS Code extension - get full path

查看:30
本文介绍了VS Code 扩展 - 获取完整路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为 VS Code 编写一个插件,我需要知道调用扩展的文件的路径,无论是从编辑器上下文菜单或资源管理器上下文菜单调用,还是用户简单地键入扩展命令.

I am writing a plugin for VS Code and I need to know the path of the file which is calling the extension, either if it was called from the editor context menu or explorer context menu or the user simply typed the extension command.

function activate(context){
    // get full path of the file somehow
}

提前致谢!

推荐答案

如果你需要文件使用 uri.fsPath如果您需要工作区文件夹,请使用 uri.path

If you need the File use uri.fsPath If you need the Workspace Folder use uri.path

if(vscode.workspace.workspaceFolders !== undefined) {
    let wf = vscode.workspace.workspaceFolders[0].uri.path ;
    let f = vscode.workspace.workspaceFolders[0].uri.fsPath ; 

    message = `YOUR-EXTENSION: folder: ${wf} - ${f}` ;

    vscode.window.showInformationMessage(message);
} 
else {
    message = "YOUR-EXTENSION: Working folder not found, open a folder an try again" ;

    vscode.window.showErrorMessage(message);
}

更多细节可以从 VS Code API

这篇关于VS Code 扩展 - 获取完整路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆