Find parent or parents directories of a file
I have a file say xyz.doc .I want to find the parent /Parents of this file ,so that i can bind it in a tree view . How can we achive this ?
I have the fileinfo class which is obtained by t开发者_StackOverflowhe code
FileInfo[] files = Directory.GetFiles(path,"*.*);
Try
LookingFor(new FileInfo(path).Directory));
public void LookingFor(DirectoryInfo dir)
{
if (dir.Parent == null)
return;
// Add parent to ListView
LookingFor(dir.Parent);
}
Take the path of the directory, and call the GetParent
method. You can do this in a loop if need be and walk back up the directory tree:
string parentDir = Directory.GetParent("some path");
精彩评论