Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

c# - What determines the return value of Path.GetTempPath()?

Currently, I use Path.GetTempPath() to figure out where to write my log files, but recently I came across a user's machine where the path returned was not what I expected.

Usually, the returned path is C:Documents and Settings[userid]Local SettingsTemp but in this case, it was C:Temp

This would not normally be a problem, but for some reason the user in question did not have access to write to C:Temp

I double checked the environment variables, and the USER environment variable was pointing as expected to C:Documents and Settings[userid]Local SettingsTemp, whilst the SYSTEM environment variable was pointing to C:WINNTTemp.

So... where is Path.GetTempPath() getting it's value from ? Group Policy? Registry?

I have Googled, but to no avail.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(Using Reflector) Path.GetTempPath() ultimately calls the Win32 function GetTempPath (from kernel32.dll). The MDSN docs for this state:

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.

Note that they also state that it doesn't check whether or not the path actually exists or can be written to, so you may end up trying to write your log files to a path that doesn't exist, or one that you cannot access.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...