Basic Examples (2) 
Evaluate code and restore the current working directory when complete:
Verify that the current directory is restored:
Specify a working directory to start from:
The file was created in the specified directory:
Properties and Relations (4) 
SetDirectory pushes a directory onto the DirectoryStack[]:
Any changes made to DirectoryStack[] during evaluation of DirectoryBlock[expr] are reverted when evaluation of expr completes:
Verify that the original directory stack is restored:
Restore the original working directory:
Use the resource function ResetDirectoryStack to set the current Directory[] and DirectoryStack[] back to their initial values:
Verify:
DirectoryBlock essentially uses ResetDirectoryStack to restore directory state:
Change the current working directory:
Using SetDirectory for both changing to a directory and returning to the original leaves undesirable directories in DirectoryStack[]:
This restores the original directory, but leaves extra paths on the DirectoryStack[]:
As a result, ResetDirectory[] will not return to the original location:
Completely reset the DirectoryStack[]:
Set the current working directory again:
Now use DirectoryBlock when changing to the temporary directory:
Both Directory[] and DirectoryStack[] are restored:
This means ResetDirectory[] will work as expected: