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

发布于 2024-07-23 11:53:41 字数 529 浏览 3 评论 0原文

目前我正在使用以下函数来获取当前用户的临时文件夹路径:

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

在某些计算机上,它为我提供当前用户的临时文件夹路径,例如:

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

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

C:\Windows\TEMP

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

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

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

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\

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美胚控场 2024-07-30 11:53:41

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

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

从该页面复制:

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

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

我不太清楚“Windows 目录”是指 windows 下的临时目录还是 windows 目录本身。 将临时文件转储到 Windows 目录本身听起来像是一种不良情况,但谁知道呢。

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

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

Have a look at 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.

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.

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.

鸠魁 2024-07-30 11:53:41

不要使用这个:

System.Environment.GetEnvironmentVariable("TEMP")

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

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

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.

蓝眼睛不忧郁 2024-07-30 11:53:41

我有同样的要求 - 我们希望将日志放入环境中应存在的特定根目录中。

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

如果我想将其与子目录组合,我应该能够使用 Path.Combine( ... )

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文