I have a path “$/Folder1/Folder2/Folder3/File.xml” I would like to get the path minus “File.xml” i.e. “$/Folder1/Folder2/Folder3”.
I have written the following method,
public string GetFilePathFromFolderPath(string serverPath)
{
var folders = serverPath.Split('/').ToList();
folders.RemoveAt(folders.Count - 1);
return folders.Aggregate(string.Empty,
(current, folder) =>
!string.IsNullOrEmpty(current)
? string.Format("{0}/{1}", current, folder)
: string.Format("{0}", folder));
}
Is there a better way to do this?
My Unit Test works fine but I would like to know if there is a simple way…
[TestMethod()]
public void GetRootPathFromConfigFilePath_Validate()
{
var t = new Twrar();
var a = t.GetFilePathFromFolderPath("$/Quan/Maa/CSr/mai.py");
Assert.IsTrue(a == "$/Quan/Maa/CSr");
}
I assume you mean to use “/” as the path separator character even though it is “\” for Windows.
outputs
$/Quan/Maa/CSrThe following is Tarun Arora’s edit:
For C# this should be…
And all of my unit tests pass this…