问题描述
我有关于网络上的位置的字符串,我需要得到从这个位置2了目录。
该字符串可能在格式为:
字符串networkDir =\\\\\\
etworkLocation\\staff\\users\\ \\\用户名;
在这种情况下,我需要的工作人员
文件夹,可以使用以下逻辑:
字符串parentDir1 = Path.GetDirectoryName(networkDir);
串parentDir2 = Path.GetPathRoot(Path.GetDirectoryName(networkDir));
然而,如果字符串的格式为:
字符串networkDir =\\\\\\
etworkLocation\\users\\username;
我只需要在 networkLocation
部分, parentDir2
返回null。
我怎样才能做到这一点?
只是为了澄清:在根恰好从给定文件夹的目录2起来的话,那么这就是我需要返回
DirectoryInfo的D =新的DirectoryInfo(\\\\\\
etworkLocation\\test\\test);
如果(d.Parent.Parent!= NULL)
{
串UP2 = d.Parent.Parent.ToString();
}
,否则
{
串UP2 = d.Root.ToString()分割(Path.DirectorySeparatorChar)[2]。
}
就是我一直在寻找。道歉造成的任何混乱!
I have a string relating to a location on a network and I need to get the directory that is 2 up from this location.
The string could be in the format:
string networkDir = "\\\\networkLocation\\staff\\users\\username";
In which case I would need the staff
folder and could use the following logic:
string parentDir1 = Path.GetDirectoryName(networkDir);
string parentDir2 = Path.GetPathRoot(Path.GetDirectoryName(networkDir));
However, if the string is in the format:
string networkDir = "\\\\networkLocation\\users\\username";
I would just need the networkLocation
part and parentDir2
returns null.
How can I do this?
Just to clarify: In the case that the root happens to be the directory 2 up from the given folder then this is what I need to return
DirectoryInfo d = new DirectoryInfo("\\\\networkLocation\\test\\test");
if (d.Parent.Parent != null)
{
string up2 = d.Parent.Parent.ToString();
}
else
{
string up2 = d.Root.ToString().Split(Path.DirectorySeparatorChar)[2];
}
Is what I was looking for. Apologies for any confusion caused!
这篇关于获取父目录的父目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!