In .NET (VB or C#) Does anyone know a simple way to remove the ‘head’ directory from a file path String, meaning if I have path that looks something like this: Directory1/Directory2/Directory3I want to get Directory2/Directory3 back. I know there are some ways to do it, like split it into an array, and then starting with the second element concatenate it all back, I just feel like that, that’s its an extremely inefficient way of doing it and was wondering if there is a better way to do this.
In .NET (VB or C#) Does anyone know a simple way to remove the
Share
It depends on what you’re looking for. If you know that things are in the form
dir1/dir2/dir3/dir4..., then you can just look for the first/and take everything after that:If you can also accept full path names of the form
c:\dir\dir\file.extor\\server\dir\dir\file.ext, then you’ll probably want to make sure that you make any relative paths into full paths first. Then use the methods in theSystem.IO.Pathclass to extract the drive or server names before using something like theIndexOftrick above.