C# Path.GetTempPath() 的返回值由什么决定?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2365307/
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
What determines the return value of Path.GetTempPath()?
提问by Andy Blackman
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.
目前,我习惯于Path.GetTempPath()
弄清楚在哪里写我的日志文件,但最近我遇到了一个用户的机器,它返回的路径不是我所期望的。
Usually, the returned path is C:\Documents and Settings\[userid]\Local Settings\Tempbut in this case, it was C:\Temp
通常,返回的路径是C:\Documents and Settings\[userid]\Local Settings\Temp但在这种情况下,它是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
这通常不是问题,但由于某种原因,相关用户无权写入C:\Temp
I double checked the environment variables, and the USER environment variable was pointing as expected to C:\Documents and Settings\[userid]\Local Settings\Temp, whilst the SYSTEM environment variable was pointing to C:\WINNT\Temp.
我仔细检查了环境变量,USER 环境变量按预期指向C:\Documents and Settings\[userid]\Local Settings\Temp,而 SYSTEM 环境变量指向C:\WINNT\Temp。
So... where is Path.GetTempPath()
getting it's value from ? Group Policy? Registry?
那么......从哪里Path.GetTempPath()
获得它的价值?组策略?注册表?
I have Googled, but to no avail.
我已经谷歌了,但无济于事。
采纳答案by adrianbanks
(Using Reflector) Path.GetTempPath()
ultimately calls the Win32 function GetTempPath(from kernel32.dll). The MDSN docs for this state:
(使用 Reflector)Path.GetTempPath()
最终调用 Win32 函数GetTempPath(来自 kernel32.dll)。此状态的 MDSN 文档:
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 目录。
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.
请注意,他们还声明它不会检查路径是否实际存在或可以写入,因此您最终可能会尝试将日志文件写入不存在的路径或您无法访问的路径.
回答by Arve
It calls the GetTempPathfunction. The documentation explains what environment variables it checks.
它调用GetTempPath函数。该文档解释了它检查的环境变量。
回答by Tom
I've noticed GetTempPath() can bring back the local user's Documents & Settings\user\Local Settings\Temp path if it's a console application, and noticed it can bring back C:\WINDOWS\Temp (on the server) if it's a web app being ran from a client. In the former case, no big deal - the account running the app has the rights to that folder. In the latter, maybe it is a big deal if the App Pool Identity account (or account you may be using to impersonate with in the Web.config file for the web app) doesn't have privileges to C:\WINDOWS\Temp on the server (which is a big chance it doesn't). So for my console apps, just so there's no question where temp files are written, hard-coding a string into an INI file is the best and easiest for me, and for a web app, hard-coding it in the web.config and getting it using ConfigurationManager.AppSettings["myKey"] works, or if it's a web app, use this function to send the file to the ASP Temporary Files folders and work with it there:
我注意到 GetTempPath() 可以带回本地用户的 Documents & Settings\user\Local Settings\Temp 路径,如果它是一个控制台应用程序,并且注意到它可以带回 C:\WINDOWS\Temp(在服务器上)如果它是一个从客户端运行的 Web 应用程序。在前一种情况下,没什么大不了的 - 运行该应用程序的帐户有权访问该文件夹。在后者中,如果 App Pool Identity 帐户(或您可能用来在 Web 应用程序的 Web.config 文件中模拟的帐户)没有 C:\WINDOWS\Temp 的权限,这可能是一个大问题服务器(这是一个很大的机会,它没有)。因此,对于我的控制台应用程序,毫无疑问,临时文件的写入位置,将字符串硬编码到 INI 文件中对我来说是最好和最简单的,而对于网络应用程序,在网络中对其进行硬编码。
public static string findFileDirectory(string file)
{
// Get the directory where our service is being run from
string temppath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
// Ensure proper path notation so we can add the INI file name
if (!temppath.EndsWith(@"\")) temppath += @"\";
return temppath;
}
and call it like this:
并这样称呼它:
string tempFolderPath = findFileDirectory("Web.config");
tempFolderPath = tempFolderPath.Replace(@"\", @"\");
and just use "tempFolderPath" instead of where you used Path.GetTempPath() before. This function works awesome & I use it in my code in place of this evil GetTempPath() method so I know my app can do what it needs to do, since the ASP Temp Files folder should have all the permissions it needs for its operations (DOMAIN\NETWORK SERVICE and App Pool ID account need Full Control). tempFolderPath ends in a trailing slash, so just concat directly with your variable/file name to get the right path going.
只需使用“tempFolderPath”而不是您之前使用 Path.GetTempPath() 的地方。这个函数工作得很好,我在我的代码中使用它代替了这个邪恶的 GetTempPath() 方法,所以我知道我的应用程序可以做它需要做的事情,因为 ASP Temp Files 文件夹应该具有它的操作所需的所有权限( DOMAIN\NETWORK SERVICE 和 App Pool ID 帐户需要完全控制)。tempFolderPath 以斜杠结尾,因此只需直接与您的变量/文件名连接即可获得正确的路径。
-Tom
-汤姆
P.S. You need to add 2 namespaces to make that function work: System.IO and System.Reflection
PS 您需要添加 2 个命名空间才能使该功能正常工作:System.IO 和 System.Reflection
回答by Wojtek Turowicz
Please try using the following to determine good place for Your data:
请尝试使用以下方法来确定您的数据的好位置:
Environment.GetFolderPath(Environment.SpecialFolder folder);
Where Specialfolder
哪里特殊文件夹
// Summary:
// Specifies enumerated constants used to retrieve directory paths to system
// special folders.
[ComVisible(true)]
public enum SpecialFolder
{
// Summary:
// The logical Desktop rather than the physical file system location.
Desktop = 0,
//
// Summary:
// The directory that contains the user's program groups.
Programs = 2,
//
// Summary:
// The directory that serves as a common repository for documents.
Personal = 5,
//
// Summary:
// The "My Documents" folder.
MyDocuments = 5,
//
// Summary:
// The directory that serves as a common repository for the user's favorite
// items.
Favorites = 6,
//
// Summary:
// The directory that corresponds to the user's Startup program group.
Startup = 7,
//
// Summary:
// The directory that contains the user's most recently used documents.
Recent = 8,
//
// Summary:
// The directory that contains the Send To menu items.
SendTo = 9,
//
// Summary:
// The directory that contains the Start menu items.
StartMenu = 11,
//
// Summary:
// The "My Music" folder.
MyMusic = 13,
//
// Summary:
// The directory used to physically store file objects on the desktop.
DesktopDirectory = 16,
//
// Summary:
// The "My Computer" folder.
MyComputer = 17,
//
// Summary:
// The directory that serves as a common repository for document templates.
Templates = 21,
//
// Summary:
// The directory that serves as a common repository for application-specific
// data for the current roaming user.
ApplicationData = 26,
//
// Summary:
// The directory that serves as a common repository for application-specific
// data that is used by the current, non-roaming user.
LocalApplicationData = 28,
//
// Summary:
// The directory that serves as a common repository for temporary Internet files.
InternetCache = 32,
//
// Summary:
// The directory that serves as a common repository for Internet cookies.
Cookies = 33,
//
// Summary:
// The directory that serves as a common repository for Internet history items.
History = 34,
//
// Summary:
// The directory that serves as a common repository for application-specific
// data that is used by all users.
CommonApplicationData = 35,
//
// Summary:
// The System directory.
System = 37,
//
// Summary:
// The program files directory.
ProgramFiles = 38,
//
// Summary:
// The "My Pictures" folder.
MyPictures = 39,
//
// Summary:
// The directory for components that are shared across applications.
CommonProgramFiles = 43,
}
回答by Simon_Weaver
Disclaimer: Not an answer - but important reading !
免责声明:不是答案 - 但重要的阅读!
It's very important to realize that you need to clear up your temp files becuase when you hit 65536 in a single directory the framework won't create any more and your app will blow up!
意识到您需要清除临时文件非常重要,因为当您在单个目录中点击 65536 时,框架将不再创建,并且您的应用程序将崩溃!
They will accumulate over months and months and then you'll get a message like this:
它们会累积数月,然后您会收到如下消息:
System.IO.IOException: The file exists.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.__Error.WinIOError()
at System.IO.Path.InternalGetTempFileName(Boolean checkHost)
at System.IO.Path.GetTempFileName():
and TFS will give you this when you try to build:
当你尝试构建时,TFS 会给你这个:
TF215097: An error occurred while initializing a build for build
definition XXXXX: The file exists.
All you need to do is do is browse to the Path.GetTempPath()
folder and call del tmp*
您需要做的就是浏览到Path.GetTempPath()
文件夹并调用del tmp*
Note: If you have an ASP.NET appliation creating temp files its temp directory will probably be different than the current logged in user
注意:如果您有一个 ASP.NET 应用程序创建临时文件,它的临时目录可能与当前登录的用户不同
If in doubt (or in panic) just create an aspx page to print out the location being used:
如果有疑问(或恐慌),只需创建一个 aspx 页面来打印出正在使用的位置:
TempPath.aspx
<%@ Page Language="C#"%>
Temp path: <%= System.IO.Path.GetTempPath() %>
For me when running as NetworkService
I get
对我来说,运行的时候为NetworkService
我得到
C:\Windows\TEMP\
When running as an AppPool (named www.example.com) the path may be:
当作为 AppPool(名为 www.example.com)运行时,路径可能是:
C:\Users\www.example.com\AppData\Local\Temp
PS. I think this can happen even if you delete the file afterwards becasue the filename increases.
附注。我认为即使您之后删除文件也会发生这种情况,因为文件名增加了。
回答by doer_uvc
If you are using C#
on MacOS
using Mono Framework
then value returned by Path.GetTempPath()
is value of environment variable TMPDIR
.
如果您使用C#
on MacOS
using,Mono Framework
则返回的Path.GetTempPath()
值是环境变量的值TMPDIR
。
Running echo $TMPDIR
usually returns value like :
运行echo $TMPDIR
通常返回如下值:
/var/folders/{2 character random-string}/{random-string}/T