[c#] How to get temporary folder for current user

Currently I am using following function to get the temporary folder path for current user:

string tempPath = System.IO.Path.GetTempPath();

On some machines it gives me temp folder path of current user like:

C:\Documents and Settings\administrator\Local Settings\Temp\

On some machines it gives me system temp folder path like:

C:\Windows\TEMP

MSDN Documentation also says that above API returns current system's temporary folder.

Is there any other API available which gives me current user's temporary folder path like this:

C:\Documents and Settings\administrator\Local Settings\Temp\

This question is related to c# .net temporary-directory

The answer is


I have this same requirement - we want to put logs in a specific root directory that should exist within the environment.

public static readonly string DefaultLogFilePath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

If I want to combine this with a sub-directory, I should be able to use Path.Combine( ... ).

The GetFolderPath method has an overload for special folder options which allows you to control whether the specified path be created or simply verified.


try

Environment.GetEnvironmentVariable("temp");

DO NOT use this:

System.Environment.GetEnvironmentVariable("TEMP")

Environment variables can be overridden, so the TEMP variable is not necessarily the directory.

The correct way is to use System.IO.Path.GetTempPath() as in the accepted answer.


Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to temporary-directory

How to create a temporary directory? How to create a temporary directory and get the path / file name in Python How to get temporary folder for current user Cross-platform way of getting temp directory in Python How to create a temporary directory/folder in Java?