开发者

Find parent or parents directories of a file

开发者 https://www.devze.com 2023-03-31 23:00 出处:网络
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 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");
0

精彩评论

暂无评论...
验证码 换一张
取 消