Justin Alexander
Justin Alexander

Reputation: 2024

"Temp" directory in Windows, IO.getFile

I would like to write to the C:\windows\temp directory (or it's configured equivalent) inside my Firefox-addon.

https://developer.mozilla.org/en/FileGuide/FileWriting

Gives the impression that there are system independent names for these paths:

var file = IO.getFile("Desktop", "myinfo.txt");
var stream = IO.newOutputStream(file, "text");
stream.writeString("This is some text");
stream.close();

But I can't find any reference in the specified references, as to what "Desktop" points to. So that leaves me not knowing what exactly is referred to in the names given by the documentation.

How to I use IO.getFile() to open a file in the windows global temp folder?

Upvotes: 1

Views: 511

Answers (3)

traxium
traxium

Reputation: 2776

// Writing stackoverflow.txt to TEMP dir
const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {})
const path = OS.Path.join(OS.Constants.Path.tmpDir, "stackoverflow.txt")
OS.File.writeAtomic(path, "Hello, StackOverflow!", {
    encoding: "utf-8",
    tmpPath: "stackoverflow.txt.tmp", // it's not necessary but I'd recommend to use it
}).then(() => console.log(path, "has been written"))
// C:\Users\traxium\AppData\Local\Temp\stackoverflow.txt has been written

// Reading stackoverflow.txt from TEMP dir
const { OS } = Cu.import("resource://gre/modules/osfile.jsm", {})
const path = OS.Path.join(OS.Constants.Path.tmpDir, "stackoverflow.txt")
OS.File.read(path, { encoding: "utf-8" }).then(txt => console.log(txt))
// "Hello, StackOverflow!"

Upvotes: 1

Nickolay
Nickolay

Reputation: 32081

See also Code snippets: File I/O on developer.mozilla.org. It answers your question (Matthew is right, it's "TmpD"), and provides many other file-related examples.

[edit] Oh, and does IO actually work for you? I thought it was unavailable. [edit2] I added a warning at the top of the pages I could find, that mention it.

Upvotes: 2

Matthew Flaschen
Matthew Flaschen

Reputation: 285077

The keys are described here.

I believe you want TmpD, which is listed here

Upvotes: 1

Related Questions