I am writing a visual studio code extension and will be needing to write a file to whatever folder is currently opened in visual studio code (or a relative path to this root), via the extension I am writing. Relative paths are from where visual studio code is launched, therefore I can accomplish this if, as the end user, I launch code via command line from the end project directory (Windows is console Code . ). However, I would like, for a better user experience, for them to be able to open any folder, without launching code from the command line, and my extension still be aware of the working directory for the opened project, is this possible? For reference, here is the working code (that requires I launch code from command line in the working directory):
var fs = require('fs');
fs.writeFile('helloworld.txt', 'Hello World!', function (err) {
if (err) {
console.log(err);
}
});
Okay I figured it out from the api:
var root = vscode.workspace.rootPath;
That's exactly what I needed.