74

I want to use StreamWriter to write a file to the temp folder.

It might be a different path on each PC, so I tried using %temp%\SaveFile.txt but it didn't work.

How can I save to the temp folder, using environmental variables?

And for example, can I use an environmental variable for storing files in %appdata%?

crthompson
  • 15,653
  • 6
  • 58
  • 80
BlueRay101
  • 1,447
  • 2
  • 18
  • 29

5 Answers5

119
string result = System.IO.Path.GetTempPath();

https://learn.microsoft.com/en-us/dotnet/api/system.io.path.gettemppath

EkoostikMartin
  • 6,831
  • 2
  • 33
  • 62
  • OK, Thank you very much. Where can I look for more environmental variables, such as the main hard drive, user settings and appdata? – BlueRay101 Nov 21 '13 at 20:13
  • 1
    The System.Environment class will help you get environment variables and so on. Also be sure to check out the System.Environment.SpecialFolder enumeration. http://msdn.microsoft.com/en-us/library/system.environment%28v=vs.110%29.aspx – trope Nov 21 '13 at 20:39
  • So for the temp folder, I always have to use the Path.GetTempPath();, and for any other special path (like AppData or ProgramFiles) I have to use System.Environment? – BlueRay101 Nov 21 '13 at 20:53
  • Do you reckon varring this baby like the code example or is it OK to execute this method multiple times performance wise? – Tom Jan 02 '18 at 14:03
53

The Path class is very useful here.
You get two methods called

Path.GetTempFileName

Path.GetTempPath

that could solve your issue

So for example you could write: (if you don't mind the exact file name)

using(StreamWriter sw = new StreamWriter(Path.GetTempFileName()))
{
    sw.WriteLine("Your error message");
}

Or if you need to set your file name

string myTempFile = Path.Combine(Path.GetTempPath(), "SaveFile.txt");
using(StreamWriter sw = new StreamWriter(myTempFile))
{
     sw.WriteLine("Your error message");
}
Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Steve
  • 213,761
  • 22
  • 232
  • 286
  • the temp directory changes based on session, using RDP i logged in and accessed%TEMP% it directs to temp/2 later temp/4 ...how to get the temp folder ( not temp / digit ) – asvignesh Mar 21 '16 at 06:47
  • 3
    Don't ask a question inside a comment on a three years old answer. Post (after a mandatory research) your own question, specifying in what is different from here – Steve Mar 21 '16 at 08:17
  • 3
    In case you don't care about the name, but do care about the extension: `Path.ChangeExtension(Path.GetTempFileName(), "foo")` – Ohad Schneider Sep 10 '18 at 22:56
  • Often easier to use `string contents = File.ReadAllText(path)` and `File.WriteAllText(path, contents)`. – Mike Lowery Jun 30 '20 at 00:01
9

You can dynamically retrieve a temp path using as following and better to use it instead of using hard coded string value for temp location.It will return the temp folder or temp file as you want.

string filePath = Path.Combine(Path.GetTempPath(),"SaveFile.txt");

or

Path.GetTempFileName();
Thilina H
  • 5,754
  • 6
  • 26
  • 56
5
System.IO.Path.GetTempPath()

The path specified by the TMP environment variable. The path specified by the TEMP environment variable. The path specified by the USERPROFILE environment variable. The Windows directory.

Sampada
  • 2,931
  • 7
  • 27
  • 39
Pacman
  • 2,183
  • 6
  • 39
  • 70
2

For %appdata% take a look to

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)