且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何为 Visual Studio Code 扩展创建文件?

更新时间:2023-11-15 13:58:46

看起来像 vscode.Uri 不支持相对路径(此处 是相应的问题).话虽如此,您必须使用绝对路径.以下代码段应该可以工作(在使用 vscode v1.30.0 的 Windows 上测试)

It seems like the vscode.Uri does not support relative paths (here is the corresponding issue). With that said you have to use an absolute path. The following snippet should work (tested on windows with vscode v1.30.0)

const wsedit = new vscode.WorkspaceEdit();
const wsPath = vscode.workspace.workspaceFolders[0].uri.fsPath; // gets the path of the first workspace folder
const filePath = vscode.Uri.file(wsPath + '/hello/world.md');
vscode.window.showInformationMessage(filePath.toString());
wsedit.createFile(filePath, { ignoreIfExists: true });
vscode.workspace.applyEdit(wsedit);
vscode.window.showInformationMessage('Created a new file: hello/world.md');