gpt4 book ai didi

c# - 如何从当前文件路径向上导航一个文件夹?

转载 作者:太空狗 更新时间:2023-10-29 22:09:09 26 4
gpt4 key购买 nike

我需要从一个文件的当前路径向上导航一个文件夹,并将同一个文件保存在那里。如何从目录路径中删除一层?谢谢!

C:\Users\stacy.zim\AppData\Local\Temp\ICLocal\e53486af-7e5e-4c54-b9dc-d15cb55f3f55.pdf

文件将保存到下面。

C:\Users\stacy.zim\AppData\Local\Temp\e53486af-7e5e-4c54-b9dc-d15cb55f3f55.pdf

最佳答案

其实它叫做Parent for "One Folder Up"

System.IO.DirectoryInfo.Parent

// Method 1 Get by file
var file = new FileInfo(@"C:\Users\stacy.zim\AppData\Local\Temp\ICLocal\e53486af-7e5e-4c54-b9dc-d15cb55f3f55.pdf");
var parentDir = file.Directory == null ? null : file.Directory.Parent; // null if root
if (parentDir != null)
{
// Do something with Path.Combine(parentDir.FullName, filename.Name);
}

System.IO.Directory.GetParent()

// Method 2 Get by path
var parentDir = Directory.GetParent(@"C:\Users\stacy.zim\AppData\Local\Temp\ICLocal\");

关于c# - 如何从当前文件路径向上导航一个文件夹?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30991331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com