2016-12-13 125 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); 
});