什么决定了Path.GetTempPath()的返回值?

目前,我使用Path.GetTempPath()来确定在哪里写我的日志文件,但最近我遇到了一个用户的机器,返回的path不是我所期望的。

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

这通常不会是一个问题,但由于某些原因,用户有权访问写入C:\ Temp

我再次检查环境variables,并且USER环境variables指向C:\ Documents and Settings \ [userid] \ Local Settings \ Temp ,而SYSTEM环境variables指向C:\ WINNT \ Temp

那么… Path.GetTempPath()从哪里得到它的值呢? 组策略? 注册?

我谷歌search,但无济于事。

(使用reflection器) Path.GetTempPath()最终调用Win32函数GetTempPath (来自kernel32.dll)。 MDSN文档为这个状态:

GetTempPath函数按照以下顺序检查是否存在环境variables,并使用find的第一个path:

  • 由TMP环境variables指定的path。
  • 由TEMP环境variables指定的path。
  • USERPROFILE环境variables指定的path。
  • Windows目录。

请注意,他们还声明, 它不检查path是否实际存在或可以写入 ,所以最终可能会尝试将日志文件写入不存在的path,或者不能访问的path。

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

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

他们将积累数月和数月,然后你会得到这样的消息:

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

而当你尝试构build时,TFS会给你这个:

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

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

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

如果有疑问(或恐慌),只需创build一个aspx页面

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

对我来说,当我作为NetworkService运行时得到的

  C:\Windows\TEMP\ 

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

我注意到GetTempPath()可以带回本地用户的Documents&Settings \ user \ Local Settings \ Temppath,如果它是一个控制台应用程序,并且注意到它可以带回C:\ WINDOWS \ Temp(在服务器上),如果它是Web应用程序正在从客户端运行。 在前一种情况下,没有什么大不了的 – 运行应用程序的帐户拥有该文件夹的权利。 在后者中,如果应用程序池标识帐户(或者您可能用于在Web应用程序的Web.config文件中模拟的帐户)对C:\ WINDOWS \ Temp没有权限,那么这可能是一件大事服务器(这是一个很大的机会)。 因此,对于我的控制台应用程序来说,临时文件的写入没有问题,将string硬编码为INI文件对我来说是最好的也是最简单的,对于Web应用程序,在web.config中对其进行硬编码使用ConfigurationManager.AppSettings [“myKey”]工作,或者如果它是一个Web应用程序,使用此function将文件发送到ASP临时文件文件夹,并在那里工作:

 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文件夹应该具有它的操作所需的所有权限域\networking服务和应用程序池ID帐户需要完全控制)。 tempFolderPath以结尾的斜线结束,所以直接连接你的variables/文件名来得到正确的path。

-Tom

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

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

请尝试使用以下来确定您的数据的好地方:

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