gDirectory is a pointer to the current directory.
As soon as you open a new file or change directory within a file, gDirectory points to the current directory.
In your case, before opening a new file, you can something like
TDirectory *olddir = gDirectory;
TFile f(’'newfile…")
olddir->GetList()->ls();
gDirectory->ls(); //will show all objects in f f.ls(); //same as previous statement
aha, yes, I forgot about the real meaning of gDirectory and that it is a pointer to the current dir. It works fine now again when I store the gDir pointer before I open the file.