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目录
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()方法