VSCode扩展写和打开文件

时间:2016-12-13 16:44:21

标签: file-io visual-studio-code

我的要求实际上非常简单,我想写一个文件并在vscode中打开它。但我不能让这个工作:

var content = rec[rt.fields[field]];
var filePath = path.join(vscode.workspace.rootPath, selected.label + '.' + field);
fs.writeFileSync(filePath, content, 'utf8');

var openPath = vscode.Uri.parse(filePath);
vscode.workspace.openTextDocument(openPath).then(doc => {
  vscode.window.showTextDocument(doc);
});

我收到以下错误消息,不知道这应该是什么意思:

  

无法打开c:%5CUsers%5Cmak%5C.sneditor%5Csoftpointdev1.service-now.com%5CRMCostPlanHelper.js。细节:uri'c没有型号:%5CUsers%5Cmak%5C.sneditor%5Csoftpointdev1.service-now.com%5CRMCostPlanHelper.js'也不是方案'c'的解析器。

2 个答案:

答案 0 :(得分:4)

我也遇到过类似的问题。您还可以通过执行以下操作来解决问题:

var content = rec[rt.fields[field]];
var filePath = path.join(vscode.workspace.rootPath, selected.label + '.' + field);
fs.writeFileSync(filePath, content, 'utf8');

var openPath = vscode.Uri.parse("file:///" + filePath); //A request file path
vscode.workspace.openTextDocument(openPath).then(doc => {
   vscode.window.showTextDocument(doc);
});

答案 1 :(得分:0)

发布此问题后,我立即找到答案^^

您必须使用vscode.Uri.file而不是vscode.Uri.parse

var content = rec[rt.fields[field]];
var filePath = path.join(vscode.workspace.rootPath, selected.label + '.' + field);
fs.writeFileSync(filePath, content, 'utf8');

var openPath = vscode.Uri.file(filePath);
vscode.workspace.openTextDocument(openPath).then(doc => {
  vscode.window.showTextDocument(doc);
});