Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 什么决定Path.GetTempPath()的返回值?_C#_.net_Environment Variables_Special Folders - Fatal编程技术网

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

C# 什么决定Path.GetTempPath()的返回值?,c#,.net,environment-variables,special-folders,C#,.net,Environment Variables,Special Folders,目前,我使用Path.GetTempPath()来确定在何处写入日志文件,但最近我遇到了一个用户的机器,其中返回的路径与我预期的不同 通常,返回的路径是C:\Documents and Settings\[userid]\Local Settings\Temp 但在本例中,它是C:\Temp 这通常不是问题,但由于某种原因,相关用户没有权限写入C:\Temp 我仔细检查了环境变量,用户环境变量按预期指向C:\Documents and Settings\[userid]\Local Settin

目前,我使用
Path.GetTempPath()
来确定在何处写入日志文件,但最近我遇到了一个用户的机器,其中返回的路径与我预期的不同

通常,返回的路径是C:\Documents and Settings\[userid]\Local Settings\Temp 但在本例中,它是C:\Temp

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

我仔细检查了环境变量,用户环境变量按预期指向C:\Documents and Settings\[userid]\Local Settings\Temp,而系统环境变量则指向C:\WINNT\Temp

所以。。。哪里是
Path.GetTempPath()
从中获取其值?团体政策?登记处

我已经在谷歌上搜索过了,但是没有用。

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

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

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

  • TMP环境变量指定的路径
  • 由TEMP环境变量指定的路径
  • USERPROFILE环境变量指定的路径
  • Windows目录
请注意,它们还声明,不会检查路径是否实际存在或是否可以写入,因此您可能会尝试将日志文件写入不存在的路径或无法访问的路径。

我注意到GetTempPath()如果是控制台应用程序,则可以带回本地用户的文档和设置\user\local Settings\Temp路径,如果是从客户端运行的web应用程序,则可以带回C:\WINDOWS\Temp(在服务器上)。在前一种情况下,没什么大不了的——运行应用程序的帐户拥有该文件夹的权限。在后一种情况下,如果应用程序池标识帐户(或您在Web应用程序的Web.config文件中用来模拟的帐户)没有服务器上的C:\WINDOWS\Temp权限,这可能是一个大问题(很可能没有)。因此,对于我的控制台应用程序,毫无疑问临时文件是在哪里写入的,将字符串硬编码到INI文件对我来说是最好、最简单的,对于web应用程序,在web.config中硬编码并使用ConfigurationManager获取它。AppSettings[“myKey”]有效,或者如果它是web应用程序,使用此功能可将文件发送到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(@"\\", @"\");
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():
只需使用“tempFolderPath”,而不是之前使用Path.GetTempPath()的位置。这个函数非常有用&我在代码中使用它来代替这个邪恶的GetTempPath()方法,这样我就知道我的应用程序可以做它需要做的事情,因为ASP Temp Files文件夹应该拥有其操作所需的所有权限(域\网络服务和应用程序池ID帐户需要完全控制)。tempFolderPath以斜杠结尾,因此只需直接使用变量/文件名concat即可获得正确的路径

-汤姆


另外,您需要添加两个名称空间才能使该功能正常工作:System.IO和System.Reflection

请尝试使用以下名称空间来确定数据的合适位置:

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,
}
免责声明:不是答案,而是重要的阅读! 认识到你需要清理临时文件是非常重要的,因为当你在一个目录中点击65536时,框架将不再创建,你的应用程序将爆炸

它们会不断累积,然后你会收到这样的信息:

    string tempFolderPath = findFileDirectory("Web.config");
    tempFolderPath = tempFolderPath.Replace(@"\\", @"\");
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()
文件夹并调用
deltmp*

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

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

 TempPath.aspx
 <%@ Page Language="C#"%>
 Temp path: <%= System.IO.Path.GetTempPath() %>
作为应用程序池(名为www.example.com)运行时,路径可能是:

 C:\Users\www.example.com\AppData\Local\Temp
注:我认为,由于文件名增加,即使您随后删除文件,也可能发生这种情况。

如果您在
MacOS上使用
C
使用
Mono Framework
,则
Path.GetTempPath()
返回的值是环境变量
TMPDIR
的值

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

/var/folders/{2 character random-string}/{random-string}/T

+1快速、清晰、清晰、好的问题-欢迎使用StackOverflow如果您需要一个始终可供用户写入的安全点,请查看隔离存储:@marc_s:除非启用了FIPS,否则在这种情况下,您在尝试使用时会出现异常IsolatedStorage@adrian:有趣-谢谢你的指点。我第一次听到这样的话(可能是因为在欧洲,我们并不太在乎“FIP”之类的东西)回答得很好!清晰,简洁,准确的信息,我需要。没有想过检查Win32函数文档。。。非常感谢。AndyMore的最新文档在备注下有非常相似的内容。事实并非如此,我只是重复了我的环境变量,在系统属性中检查了它们,并执行了一个Path。GetTempPath()它们不一样:今天早上在生产系统死机的情况下进了办公室。我们的一个开发人员使用Path.GetTempFileName()生成随机文件名,以便在其他地方使用。他不知道它还在c:\windows\temp中创建一个零字节文件。系统必须已尝试创建其65537文件!这一评论为我们节省了数小时的停机时间。感谢2年前的发帖!对于将来的检查:我记得Path.GetTempFileName()方法