繁体   English   中英

C#如何获取当前路径并向上移动一个文件夹

[英]C# How to get current path and go up one folder

我可以通过以下方式获取当前路径:

Request.PhysicalApplicationPath;

例如,它是C:\\ consoleapp \\ capp \\ files

我想将一个文件夹上移至:C:\\ consoleapp \\ capp \\

然后到另一个文件夹C:\\ consoleapp \\ capp \\ images

我怎么做?

您应该使用System.IO
您可以使用DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory()) ,然后仅使用di.Parent

以下内容将帮助您进入父目录,然后基于该目录创建新路径。

var path = @"C:\consoleapp\capp\files"; // or in your case Request.PhysicalApplicationPath
var parent = Directory.GetParent(path).FullName;
var newPath = Path.Combine(parent,"Images");

您可以在此处阅读有关Directory.GetParent()的更多详细信息。 Path.Combine允许您将多个字符串组合到path。 你可以在这里阅读更多关于Path.Combine

产量

parent = C:\consoleapp\capp
newPath = C:\consoleapp\capp\Images

如何向上浏览几个文件夹?

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

导航后,您可以再次附加新文件夹

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM