Search code examples
.netazurefiletemp

.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.


Solution

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

    var tempPath = RoleEnvironment.GetLocalResource("Temp").RootPath;
    Environment.SetEnvironmentVariable("TEMP", tempPath);
    Environment.SetEnvironmentVariable("TMP", tempPath);