如何向上浏览几个文件夹? [英] How to navigate a few folders up?

查看:82
本文介绍了如何向上浏览几个文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个选项是几次执行System.IO.Directory.GetParent()。有没有更优雅的方式可以从正在执行的程序所在的位置向上移动几个文件夹?

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?

我想做的就是找到一个文本文件,该文件位于上面的一个文件夹中应用程序文件夹。但是程序集本身在容器中,该容器位于应用程序文件夹的深处。

What I trying to do is to find a text file that resides one folder above the application folder. But the assembly itself is inside the bin, which is a few folders deep in the application folder.

推荐答案

其他简单方法是这样做:

Other simple way is to do this:

string path = @"C:\Folder1\Folder2\Folder3\Folder4";
string newPath = Path.GetFullPath(Path.Combine(path, @"..\..\"));

注意:此级别上升了两个级别。结果将是:
newPath = @ C:\Folder1\Folder2\;

Note This goes two levels up. The result would be: newPath = @"C:\Folder1\Folder2\";

这篇关于如何向上浏览几个文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆