System.IOUtils.TDirectory.GetParent奇怪的行为? [英] System.IOUtils.TDirectory.GetParent odd behavior?
问题描述
GetParent('c:\1\2\')
的结果是'c:\1\2 '
。
GetParent('c:\1\2')的结果
是'c:\1'
。
这是正常/预期的行为吗?如果是的话,背后的逻辑是什么?
Is this the normal/expected behavior? If yes, what is the logic behind it?
推荐答案
这个行为可能看起来有点奇怪,但是是设计的。这个类是以.net类 Directory
建模的。 的文档 Directory.GetParent
说:
This behaviour might seem a little odd but it is by design. This class is modelled on the .net class Directory
. The documentation for Directory.GetParent
says:
此方法返回的字符串由路径中的所有字符组成,但不包括最后的DirectorySeparatorChar或AltDirectorySeparatorChar。例如,将路径C:\Directory\SubDirectory\test.txt传递给GetParent返回C:\Directory\SubDirectory。传递C:\Directory\SubDirectory返回C:\Directory。但是,通过C:\Directory\SubDirectory\返回C:\Directory\SubDirectory,因为结束目录分隔符在SubDirectory之后。
The string returned by this method consists of all characters in the path up to, but not including, the last DirectorySeparatorChar or AltDirectorySeparatorChar. For example, passing the path "C:\Directory\SubDirectory\test.txt" to GetParent returns "C:\Directory\SubDirectory". Passing "C:\Directory\SubDirectory" returns "C:\Directory". However, passing "C:\Directory\SubDirectory\" returns "C:\Directory\SubDirectory", because the ending directory separator is after "SubDirectory".
这篇关于System.IOUtils.TDirectory.GetParent奇怪的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!