如何为 Visual Studio 代码扩展创建文件?
How do I create a file for a Visual Studio Code Extension?
我正在尝试创建一个文件作为我的扩展命令之一的一部分,但似乎无法正确完成。
let wsedit = new vscode.WorkspaceEdit();
const file_path = vscode.Uri.file(value + '/' + value + '.md');
vscode.window.showInformationMessage(file_path.toString());
wsedit.createFile(file_path, {ignoreIfExists: true});
vscode.workspace.applyEdit(wsedit);
vscode.window.showInformationMessage('Created a new file: ' value + '/' + value + '.md);
value
是用户输入的字符串。代码执行,但据我所知,没有文件正在创建。如何正确创建文件?
似乎vscode.Uri
does not support relative paths (here是相应的问题)。话虽如此,您必须使用绝对路径。以下代码段应该有效(在 windows 上测试 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');
我正在尝试创建一个文件作为我的扩展命令之一的一部分,但似乎无法正确完成。
let wsedit = new vscode.WorkspaceEdit();
const file_path = vscode.Uri.file(value + '/' + value + '.md');
vscode.window.showInformationMessage(file_path.toString());
wsedit.createFile(file_path, {ignoreIfExists: true});
vscode.workspace.applyEdit(wsedit);
vscode.window.showInformationMessage('Created a new file: ' value + '/' + value + '.md);
value
是用户输入的字符串。代码执行,但据我所知,没有文件正在创建。如何正确创建文件?
似乎vscode.Uri
does not support relative paths (here是相应的问题)。话虽如此,您必须使用绝对路径。以下代码段应该有效(在 windows 上测试 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');