如何使用 C# 从根文件夹路径中找到更大数量的文件夹?
How do I find greater number folder from root folder path using c#?
我在 Controllers
文件夹中有 3 个文件夹 z1
, z2
和 z3
如何比较并找出 z3
在所有列出的文件夹中更大?
此代码将给我文件夹深度。
public static int FolderDepth(string path)
{
if (string.IsNullOrEmpty(path))
return 0;
DirectoryInfo parent = Directory.GetParent(path);
if (parent == null)
return 1;
return FolderDepth(parent.FullName) + 1;
}
greater means in name like 3 is greater than 2 &1 so z3 is greater.
out put should be z3
不太确定这是否是您想要的,但我试一试:
public bool DirectoryIsGreatest(string directoryPath)
{
if (string.IsNullOrEmpty(path))
return false;
var parent = Directory.GetParent(directoryPath);
if (parent == null)
return false;
var directoriesToCheck = Directory.GetDirectories(parent.FullName).ToList();
directoriesToCheck.Sort();
return directoriesToCheck[directoriesToCheck.Count - 1] == directoryPath;
}
var directory = Directory.GetDirectories(path)
.OrderByDescending(dir => dir)
.FirstOrDefault();
现在,您可能遇到了另一个问题。 "greater"、z15
或 z2
应该是什么?计算机将如何发现?您的文件夹是否遵循某种模式?
我在 Controllers
文件夹中有 3 个文件夹 z1
, z2
和 z3
如何比较并找出 z3
在所有列出的文件夹中更大?
此代码将给我文件夹深度。
public static int FolderDepth(string path)
{
if (string.IsNullOrEmpty(path))
return 0;
DirectoryInfo parent = Directory.GetParent(path);
if (parent == null)
return 1;
return FolderDepth(parent.FullName) + 1;
}
greater means in name like 3 is greater than 2 &1 so z3 is greater. out put should be z3
不太确定这是否是您想要的,但我试一试:
public bool DirectoryIsGreatest(string directoryPath)
{
if (string.IsNullOrEmpty(path))
return false;
var parent = Directory.GetParent(directoryPath);
if (parent == null)
return false;
var directoriesToCheck = Directory.GetDirectories(parent.FullName).ToList();
directoriesToCheck.Sort();
return directoriesToCheck[directoriesToCheck.Count - 1] == directoryPath;
}
var directory = Directory.GetDirectories(path)
.OrderByDescending(dir => dir)
.FirstOrDefault();
现在,您可能遇到了另一个问题。 "greater"、z15
或 z2
应该是什么?计算机将如何发现?您的文件夹是否遵循某种模式?