views:

81

answers:

3

If i make the call File myFile = new File('myfile.txt'); where is does this get saved to?

+8  A: 

It's relative to the process's current directory. What that is will depend on how your application has been started, and what else you've done - for example, some things like the choose file dialog can change the current working directory.

EDIT: If you're after a temporary file, Path.GetTempFileName() is probably what you're after. You can get the temp folder with Path.GetTempPath().

Jon Skeet
+1. Right. Good answer as always.
David Stratton
This helped me, thanks :D
whydna
A: 

That won't compile.

EDIT: However, if you're after where creating a text file using StreamWriter or File.Create("text.txt"), etc, then I defer to the other answers above; where it will be where the application is running from. Be aware as others mentioned that if you're working out of debug it will be in the debug folder, etc.

Randster
True... although I think it's reasonably obvious what the OP is asking about.
Jon Skeet
Of course it will, as long as that character exists!
stack
@Jon Skeet: I know, but I was just stating what seemed immediately obvious to me. Sorry if my answer wasn't immediately helpful, I've edited it a bit.
Randster
A: 

NORMALLY it gets saved to the same directory the executable is running in. I've seen exceptions. (I can't remember the specifics, but it threw me for a loop. I seem to recall it being when it's run as a scheduled task, or if it's run from a shortcut.

The only reliable way to know where it is going to save if you are using the code snippet you provided is to check the value of System.Environment.CurrentDirectory. Better yet, explicitly state the path where you want it to save.

Edit - added

I know this is a bit late in modifying this question, but I found a better answer to the problem of ensuring that you always save the file to the correct location, relative to the executable. The accepted answer there is worth up-votes, and is probably relevant to your question.

See here: http://stackoverflow.com/questions/674857/should-i-use-appdomain-currentdomain-basedirectory-or-system-environment-currentd

David Stratton