为什么GetDirectoryName返回null C:\?GetDirectoryName、null

2023-09-03 06:42:41 作者:酷到炸

我刚刚碰到什么似乎像一个奇怪的设计选择:

I just came across what seems like a weird design choice:

System.IO.Path.GetDirectoryName(@"C:\folder\file.ext")

返回C:\文件夹

returns "C:\folder"

System.IO.Path.GetDirectoryName(@"C:\")

返回null。

returns null.

我能更好地理解它,如果第一个例子中返回的文件夹。

I could understand it better if the first example returned "folder".

任何想法的为什么是这样的话?什么做的您的做的时候,你只是想获得的文件夹路径?

Any ideas why this is the case? And what do you do when you just want to get the folder path?

推荐答案

从的函数的文档:

返回值类型: 一个   包含目录字符串   信息路径,或    空引用   (在Visual Basic中为Nothing),如果路径   表示根目录   

(加入由我强调)

Return Value Type: A String containing directory information for path, or null reference (Nothing in Visual Basic) if path denotes a root directory

(emphasis added by me)