如何获得当前用户临时文件夹 [英] How to get temporary folder for current user

查看:164
本文介绍了如何获得当前用户临时文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 字符串TEMPPATH = System.IO.Path.GetTempPath();
 

在某些机器上它给当前用户的我的临时文件夹的路径,如:

  

C:\ Documents和Settings \管理员\本地设置的\ Temp \

在某些机器上它给我的系统临时文件夹路径,如:

  

C:\ WINDOWS \ TEMP

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

是否有任何其他可用的API,它使我当前用户的临时文件夹路径是这样的:

  

C:\ Documents和Settings \管理员\本地设置的\ Temp \

解决方案

System.IO.Path.GetTempPath()只是一个包装一个本地调用 GetTempPath(..) Kernel32中。

看一看<一href="http://msdn.microsoft.com/en-us/library/aa364992%28VS.85%29.aspx">http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx

从该页面复制:

  

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

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

这不是完全清楚,我是否Windows目录是指在Windows或Windows目录本身的临时目录。倾倒在windows目录本身的临时文件听起来像是一个不受欢迎的情况下,但谁知道。

让网页结合您的文章,我猜想,无论是TMP,TEMP或USERPROFILE变量您的管理员用户指向窗口的路径之一,否则他们没有设置,它的服用回退到窗口临时路径。

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\

解决方案

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.

这篇关于如何获得当前用户临时文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆