How to navigate a few folders up?

前端 未结 11 2297
感动是毒
感动是毒 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:14

    if c:\folder1\folder2\folder3\bin is the path then the following code will return the path base folder of bin folder

    //string directory=System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString());
    
    string directory=System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString();
    

    ie,c:\folder1\folder2\folder3

    if you want folder2 path then you can get the directory by

    string directory = System.IO.Directory.GetParent(System.IO.Directory.GetParent(Environment.CurrentDirectory).ToString()).ToString();
    

    then you will get path as c:\folder1\folder2\

提交回复
热议问题