I need to parse a directory string I get and remove last few folders.
For example, when I have this directory string:
C:\workspace\AccurevTestStream\ComponentB\include
I m开发者_开发知识库ay need to cut the last two directores to create a new directory string:
C:\workspace\AccurevTestStream
what is a good way to do this? I know I can use string split and join but I think there may be a better way to do this.
var path = "C:\workspace\AccurevTestStream\ComponentB\include";    
DirectoryInfo d = new DirectoryInfo(path);
var result = d.Parent.Parent.FullName;
Here's a simple recursive method that assumes you know how many parent directories to remove from the path:
public string GetParentDirectory(string path, int parentCount) {
    if(string.IsNullOrEmpty(path) || parentCount < 1)
        return path;
    string parent = System.IO.Path.GetDirectoryName(path);
    if(--parentCount > 0)
        return GetParentDirectory(parent, parentCount);
    return parent;
}
You could use the System.IO.Path class in this case - if you call Path.GetDirectoryName repeatedly, it will chop off the last path:
string path = @"C:\workspace\AccurevTestStream\ComponentB\include";
path = Path.GetDirectoryName(path); //returns C:\workspace\AccurevTestStream\ComponentB
path = Path.GetDirectoryName(path); //returns C:\workspace\AccurevTestStream
//etc
You might try:
myNewString = myOriginalString.SubString(0, LastIndexOf(@"\"));
myNewString = myNewString.SubString(0, LastIndexOf(@"\"));
Not elegant, but should be effective.
Edit: (even more inelegant)
string myNewString = myOriginalString;
for(i=0;i<NumberToChop;i++)
{
    if(LastIndexOf(@"\") > 0)
        myNewString = myNewString.SubString(0, LastIndexOf(@"\"));
}
I'd go with the DirectoryInfo class and its Parent property.
http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.aspx
    static String GoUp(String path, Int32 num)
    {
        if (num-- > 0)
        {
            return GoUp(Directory.GetParent(path).ToString(), num);
        }
        return path;
    }
The easiest way to do this:
string path = @"C:\workspace\AccurevTestStream\ComponentB\include"
string newPath = Path.GetFullPath(Path.Combine(path, @"..\..\"));
Note This goes two levels up. The result would be:
    newPath = @"C:\workspace\AccurevTestStream\";
What about this (sorry, I don't know what your criteria is for determining what to delete)...
var di = new System.IO.DirectoryInfo("C:\workspace\AccurevTestStream\ComponentB\include");
while (!deleteDir)
    di = di.Parent;
di.Delete(true);
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论