【问题标题】:VSCode API check if path existsVSCode API 检查路径是否存在
【发布时间】:2019-10-18 13:25:18
【问题描述】:

在 VSCode 扩展中,如何在不打开或读取文件的情况下检查文件路径是否存在?

我找到的最接近的是vscode.workspace.fs.Readfilevscode.workspace.openTextDocument (https://code.visualstudio.com/api/references/vscode-api#FileSystem),但同样,我不想打开或阅读该文件。有没有办法做到这一点?

【问题讨论】:

    标签: visual-studio-code vscode-extensions


    【解决方案1】:

    根据@realappie 对另一个答案的评论,您可以使用vscode.workspace.fs

    它没有像exists 这样的方法,但你可以使用.stat()。来自the repo的例子:

    try {
        await vscode.workspace.fs.stat(jsUri);
        vscode.window.showTextDocument(jsUri, { viewColumn: vscode.ViewColumn.Beside });
    } catch {
        vscode.window.showInformationMessage(`${jsUri.toString(true)} file does *not* exist`);
    }
    

    【讨论】:

      【解决方案2】:

      因为有一个内置函数,所以没有 API:

      import * as fs from 'fs'; // In NodeJS: 'const fs = require('fs')'
      if (fs.existsSync(path)) {
        // File exists in path
      }
      

      【讨论】:

      • 不确定是否仍然如此。查看新的 vscode fs API 说明 here
      • 不遵循虚拟工作区的建议。
      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 2019-04-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-28
      相关资源
      最近更新 更多