30

What's the most concise (but safe) way to remove a drive name, network path, etc. from an absolute path in C#?

For example, converting

\\networkmachine\foo\bar

or

C:\foo\bar

to \foo\bar.

There seem to be a good number of questions already answered with regards to path matters, but I couldn't quite find what I was looking for. My own first thought that came to mind was to use Path.GetFullPath() to ensure I'm indeed working with an absolute path and then to just use a regular expression to find the first slash that isn't next to another one. However, using a regular expression to do path manipulation seems slightly dangerous.

Would it perhaps be wiser to get the drive letter/target network machine/etc, convert the strings to Uri, and ask for the path relative to the drive/machine, and then convert back to strings? Or is there an even better approach?

4
  • 1
    I've written my own helper functions that deal with this. Just some if...else and string operations.
    – Uwe Keim
    Oct 14, 2011 at 19:21
  • 1
    Then it may be nice to share those methods with us. Or else, your comment was useless :)
    – Jan Sverre
    Oct 14, 2011 at 19:23
  • 3
    You could strip the string returned from Path.GetPathRoot() from the beginning of the path, but that would also strip foo from the network-path, since this is the share-name.
    – DeCaf
    Oct 14, 2011 at 19:23
  • 2
    Or if you are doing much of these manipulations you may want to check out alphafs.codeplex.com which contains some additional path manipulation options to those provided by .NET along with support for extended paths (also not available in standard .NET)
    – DeCaf
    Oct 14, 2011 at 19:31

5 Answers 5

44

use

string MyPath = @""; // \\networkmachine\foo\bar OR C:\foo\bar
string MyPathWithoutDriveOrNetworkShare = MyPath.Substring (Path.GetPathRoot(MyPath).Length);

Result for C:\foo\bar would be foo\bar and for \\networkmachine\foo\bar would be bar.

For MSDN reference see http://msdn.microsoft.com/en-us/library/system.io.path.getpathroot.aspx

EDIT - as per comments:

With "string voodoo" (which is NOT concise IMHO and thus NOT recommended) you could do this:

if ( ( MyPath.IndexOf (":") == 1 ) || ( MyPath.IndexOf ( "\\\\" ) == 0 ) )
     { MyPathWithoutDriveOrNetworkShare = MyPath.Substring (2); }
if ( MyPathWithoutDriveOrNetworkShare.IndexOf ( "\\" ) > 0 )
     MyPathWithoutDriveOrNetworkShare = MyPathWithoutDriveOrNetworkShare.Substring ( MyPathWithoutDriveOrNetworkShare.IndexOf ( "\\" ) );  
1
  • 10
    But that wouldn't give the path he asked for in the network path sample above, since GetPathRoot would return \\networkmachine\foo in this case.
    – DeCaf
    Oct 14, 2011 at 19:29
2

Have you looked at the DirectoryInfo class?

Specifically DirectoryInfo.Parent and DirectoryInfo.Root may help at discovering the root directory so you can remove it from FullName

Parent: http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.parent.aspx

Root: http://msdn.microsoft.com/en-us/library/system.io.directoryinfo.root.aspx

0

I don't know about safety, but I'd personally put it into a string, look for a contains("\\") or ':' and remove them from the string by substringing around them.

0

As of request, here is how I did it:

I've written a helper class PathHelper that does what I think that the asker wants to know.

You can find the library on CodeProject, the function I would use is PathHelper.GetDriveOrShare, in a manner similar to:

var s = @"C:\foo\bar";
var withoutRoot = s.Substring( PathHelper.GetDriveOrShare(s).Length ); 
0

I think this will work
this code using call recursion function

this only used for folder or file path

//unRootedPath= PathHelper.GetPathWithoutRoot("C:\foo\bar"); => "foo\bar"

 public static class PathHelper
{

     public static string GetPathWithoutRoot(string orgPath)
    {
        if (!Path.IsPathRooted(orgPath))
        {
            return orgPath;
        }
        return GetPathWithoutRoot(new DirectoryInfo(orgPath));
    }

    public static string GetPathWithoutRoot(DirectoryInfo dirInfo)
    {
        if (dirInfo.Parent == null)
        {
            return string.Empty;//here recursion will stop
        }

        var parent = GetPathWithoutRoot(dirInfo.Parent);//Call same function (Recursion)
        if (string.IsNullOrEmpty(parent))
        {
            return dirInfo.Name;//here start write path without root("C:\")
        }
        else
        {
            return Path.Combine(parent, dirInfo.Name);
        }
    }
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Aug 13, 2022 at 5:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.