75

目前,我Path.GetTempPath()过去常常弄清楚在哪里写入我的日志文件,但最近我遇到了一个用户的机器,其中返回的路径不是我所期望的。

通常,返回的路径是C:\Documents and Settings\[userid]\Local Settings\Temp 但在这种情况下,它是C:\Temp

这通常不是问题,但由于某种原因,相关用户无权写入C:\Temp

我仔细检查了环境变量,并且 USER 环境变量按预期指向C:\Documents and Settings\[userid]\Local Settings\Temp,而 SYSTEM 环境变量指向C:\WINNT\Temp

那么......从哪里Path.GetTempPath()获得它的价值?组策略?注册表?

我已经谷歌了,但无济于事。

4

6 回答 6

64

(使用 Reflector)Path.GetTempPath()最终调用 Win32 函数GetTempPath(来自 kernel32.dll)。此状态的 MDSN 文档:

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

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

请注意,他们还声明它不会检查路径是否实际存在或是否可以写入,因此您最终可能会尝试将日志文件写入不存在的路径,或者您无法访问的路径.

于 2010-03-02T17:42:51.903 回答
17

免责声明:不是答案 - 但重要的阅读!

意识到您需要清除临时文件非常重要,因为当您在单个目录中点击 65536 时,框架将不再创建,您的应用程序将崩溃!

它们将累积数月和数月,然后您将收到如下消息:

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():

当您尝试构建时,TFS 会为您提供:

TF215097: An error occurred while initializing a build for build 
definition XXXXX: The file exists. 

您需要做的就是浏览Path.GetTempPath()文件夹并调用del tmp*

注意:如果您有一个创建临时文件的 ASP.NET 应用程序,它的临时目录可能与当前登录的用户不同

如果有疑问(或恐慌),只需创建一个 aspx 页面以打印出正在使用的位置:

 TempPath.aspx
 <%@ Page Language="C#"%>
 Temp path: <%= System.IO.Path.GetTempPath() %>

对我来说,当我跑步NetworkService

 C:\Windows\TEMP\

当作为 AppPool(名为 www.example.com)运行时,路径可能是:

 C:\Users\www.example.com\AppData\Local\Temp

PS。我认为即使您之后删除文件也会发生这种情况,因为文件名会增加。

于 2013-07-17T02:08:33.413 回答
3

如果您使用C#on MacOSusingMono Framework则返回的Path.GetTempPath()值是环境变量的值TMPDIR

运行echo $TMPDIR通常会返回如下值:

/var/folders/{2 character random-string}/{random-string}/T
于 2018-04-09T09:26:39.973 回答
2

我注意到 GetTempPath() 可以带回本地用户的 Documents & Settings\user\Local Settings\Temp 路径,如果它是控制台应用程序,并注意到它可以带回 C:\WINDOWS\Temp (在服务器上),如果它是从客户端运行的 Web 应用程序。在前一种情况下,没什么大不了的 - 运行应用程序的帐户拥有该文件夹的权限。在后者中,如果 App Pool Identity 帐户(或您可能在 Web 应用程序的 Web.config 文件中用来模拟的帐户)没有 C:\WINDOWS\Temp 的权限,这可能是一件大事服务器(这是一个很大的机会,它没有)。因此,对于我的控制台应用程序,毫无疑问,将临时文件写入何处,将字符串硬编码到 INI 文件中对我来说是最好和最简单的,而对于 Web 应用程序,在 Web 中对其进行硬编码。

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;
}

并这样称呼它:

    string tempFolderPath = findFileDirectory("Web.config");
    tempFolderPath = tempFolderPath.Replace(@"\\", @"\");

并且只需使用“tempFolderPath”而不是之前使用 Path.GetTempPath() 的位置。这个函数非常棒,我在我的代码中使用它来代替这个邪恶的 GetTempPath() 方法,所以我知道我的应用程序可以做它需要做的事情,因为 ASP Temp Files 文件夹应该拥有它操作所需的所有权限( DOMAIN\NETWORK SERVICE 和 App Pool ID 帐户需要完全控制)。tempFolderPath 以斜杠结尾,因此只需直接与您的变量/文件名连接即可获得正确的路径。

-汤姆

PS 您需要添加 2 个命名空间才能使该功能正常工作:System.IO 和 System.Reflection

于 2010-06-22T21:05:36.890 回答
2

请尝试使用以下方法确定您的数据的好位置:

Environment.GetFolderPath(Environment.SpecialFolder folder);

哪里特别文件夹

// 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,
}
于 2010-07-19T18:28:06.890 回答
1

它调用GetTempPath函数。该文档解释了它检查的环境变量。

于 2010-03-02T17:42:31.760 回答