4

How to set temp path for this two methods?

System.IO.Path.GetTempFileName()
System.IO.Path.GetTempPath()

My company application was designed for Windows 2008 with .NET 4.0. However the application is going to support both Windows 2008 and Azure.

Since Azure does not allow local file writes, no temp file can be created in Azure. In the application, there are many places using temp file for massive works (that means we cannot put the data in memory since the temp file is huge.)

My plan is going to create a TempFileWrapper to replace the original temp file generation. However, if there is simply way to change the return values from System.IO.Path.GetTempFileName() and System.IO.Path.GetTempPath, that saves my works.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Alex Yeung
  • 2,495
  • 7
  • 31
  • 48

2 Answers2

8

Blatantly copied from this blog post, 3rd google hit:

var tempPath = RoleEnvironment.GetLocalResource("Temp").RootPath;
Environment.SetEnvironmentVariable("TEMP", tempPath);
Environment.SetEnvironmentVariable("TMP", tempPath);
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
5

The MSDN documentation describes how GetTempPath finds the path:

  1. The path specified by the TMP environment variable.
  2. The path specified by the TEMP environment variable.
  3. The path specified by the USERPROFILE environment variable.
  4. The Windows directory.

So simply change the TMP or TEMP environment variable.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
EMP
  • 59,148
  • 53
  • 164
  • 220