Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Change Temp Path

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.

like image 340
Alex Yeung Avatar asked Dec 12 '22 17:12

Alex Yeung


2 Answers

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

var tempPath = RoleEnvironment.GetLocalResource("Temp").RootPath;
Environment.SetEnvironmentVariable("TEMP", tempPath);
Environment.SetEnvironmentVariable("TMP", tempPath);
like image 198
Hans Passant Avatar answered Dec 31 '22 12:12

Hans Passant


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.

like image 24
EMP Avatar answered Dec 31 '22 12:12

EMP