如何获取当前用户的临时文件夹
目前我正在使用以下函数来获取当前用户的临时文件夹路径:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
System.IO.Path.GetTempPath()
只是 Kernel32 中对GetTempPath(..)
的本机调用的包装器。查看 http://msdn.microsoft.com /en-us/library/aa364992(VS.85).aspx
从该页面复制:
我不太清楚“Windows 目录”是指 windows 下的临时目录还是 windows 目录本身。将临时文件转储到 Windows 目录本身听起来像是一种不良情况,但谁知道呢。
因此,将该页面与您的帖子结合起来,我猜测管理员用户的 TMP、TEMP 或 USERPROFILE 变量之一指向 Windows 路径,否则它们未设置,并且会回退到 Windows 临时路径。
System.IO.Path.GetTempPath()
is just a wrapper for a native call toGetTempPath(..)
in Kernel32.Have a look at http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx
Copied from that page:
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.
不要使用这个:
环境变量可以被覆盖,因此
TEMP
变量不一定是目录。正确的方法是使用
System.IO.Path.GetTempPath()
如接受的答案中所示。DO NOT use this:
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.我有同样的要求 - 我们希望将日志放入环境中应存在的特定根目录中。
如果我想将其与子目录组合,我应该能够使用
Path.Combine( ... )
。GetFolderPath
方法具有特殊文件夹选项的重载,它允许您控制是创建还是简单地验证指定的路径。I have this same requirement - we want to put logs in a specific root directory that should exist within the environment.
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.