2016-12-13 124 views
0

我的要求其实很简单,我想写一个文件并在vscode中打开它。但我不能得到这个工作: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'的解析器。

回答

0

只要一贴这个问题,我找到了答案^^

你必须使用vscode.Uri.filevscode.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); 
}); 
2

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

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); 
});