How to navigate a few folders up?

前端 未结 11 2290
感动是毒
感动是毒 2020-11-29 20:52

One option would be to do System.IO.Directory.GetParent() a few times. Is there a more graceful way of travelling a few folders up from where the executing assembly resides?

11条回答
  •  遥遥无期
    2020-11-29 21:21

    Maybe you could use a function if you want to declare the number of levels and put it into a function?

    private String GetParents(Int32 noOfLevels, String currentpath)
    {
         String path = "";
         for(int i=0; i< noOfLevels; i++)
         {
             path += @"..\";
         }
         path += currentpath;
         return path;
    }
    

    And you could call it like this:

    String path = this.GetParents(4, currentpath);
    

提交回复
热议问题