ソースコード
private static IEnumerable<string> GetParentDirectories( string path ) { while ( true ) { var directoryName = Path.GetDirectoryName( path ); if ( string.IsNullOrWhiteSpace( directoryName ) ) yield break; yield return directoryName; path = directoryName; } }
使用例
foreach ( var parentDirectory in GetParentDirectories(path) ) { Debug.Log( parentDirectory ); }