C# 如何获取当前用户的临时文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/944483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:55:35  来源:igfitidea点击:

How to get temporary folder for current user

c#.nettemporary-directory

提问by Anoop

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\

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

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

在某些机器上,它为我提供了系统临时文件夹路径,例如:

C:\Windows\TEMP

C:\Windows\TEMP

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

MSDN 文档还说上述 API 返回当前系统的临时文件夹。

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

是否有任何其他可用的 API 可以为我提供当前用户的临时文件夹路径,如下所示:

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

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

采纳答案by Niall Connaughton

System.IO.Path.GetTempPath()is just a wrapper for a native call to GetTempPath(..)in Kernel32.

System.IO.Path.GetTempPath()只是GetTempPath(..)在 Kernel32 中本地调用的包装器。

Have a look at http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx

看看http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx

Copied from that page:

从该页面复制:

The GetTempPath function checks for the existence of environment variables in the following order and uses the first path found:

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

GetTempPath 函数按以下顺序检查环境变量是否存在,并使用找到的第一个路径:

  • TMP 环境变量指定的路径。
  • 由 TEMP 环境变量指定的路径。
  • USERPROFILE 环境变量指定的路径。
  • Windows 目录。

It's not entirely clear to me whether "The Windows directory" means the temp directory under windows or the windows directory itself. Dumping temp files in the windows directory itself sounds like an undesirable case, but who knows.

我并不完全清楚“Windows 目录”是指 Windows 下的临时目录还是 Windows 目录本身。在 windows 目录中转储临时文件本身听起来像是一种不受欢迎的情况,但谁知道呢。

So combining that page with your post I would guess that either one of the TMP, TEMP or USERPROFILE variables for your Administrator user points to the windows path, or else they're not set and it's taking a fallback to the windows temp path.

因此,将该页面与您的帖子相结合,我猜想您的管理员用户的 TMP、TEMP 或 USERPROFILE 变量之一指向 Windows 路径,否则它们未设置并且它正在回退到 Windows 临时路径。

回答by Ikke

try

尝试

Environment.GetEnvironmentVariable("temp");

回答by Helen

DO NOTuse this:

不要使用这个:

System.Environment.GetEnvironmentVariable("TEMP")

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

环境变量可以被覆盖,所以TEMP变量不一定是目录。

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

正确的方法是System.IO.Path.GetTempPath()在接受的答案中使用。

回答by IAbstract

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

如果我想将它与子目录结合起来,我应该可以使用Path.Combine( ... ).

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

GetFolderPath方法具有特殊文件夹选项的重载,允许您控制是创建指定路径还是仅验证指定路径。