119

Is there a method to check if given path is full path? Right now im doing this:

if (template.Contains(":\\")) //full path already given
{
}
else //calculate the path from local assembly
{
}

But there must be more elegant way for checking this?

9 Answers 9

156

Try using System.IO.Path.IsPathRooted? It also returns true for absolute paths.

System.IO.Path.IsPathRooted(@"c:\foo"); // true
System.IO.Path.IsPathRooted(@"\foo"); // true
System.IO.Path.IsPathRooted("foo"); // false

System.IO.Path.IsPathRooted(@"c:1\foo"); // surprisingly also true
System.IO.Path.GetFullPath(@"c:1\foo");// returns "[current working directory]\1\foo"
8
  • 23
    How come second example be absolute path?
    – om471987
    Apr 4, 2012 at 0:59
  • 9
    The second path is not absolute, however it is rooted. The leading slash indicates the root of the system.
    – detaylor
    Apr 4, 2012 at 7:48
  • 5
    @SmirkinGherkin so what is the difference between a rooted and absolute path? Mar 1, 2013 at 1:23
  • 2
    As per the documentation 'Tests if the given path contains a root. A path is considered rooted if it starts with a backslash ("\") or a drive letter and a colon (":").' So doesn't explain the difference, but the terminology. Jan 4, 2015 at 8:15
  • 1
    See my answer (stackoverflow.com/a/35046453/704808) for an alternative that ensures a full path while retaining the advantages of IsPathRooted: avoiding accessing the file system or throwing exceptions for invalid input.
    – weir
    Jan 27, 2016 at 20:53
41
Path.IsPathRooted(path)
&& !Path.GetPathRoot(path).Equals(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal)

The above condition:

  • does not require file system permissions
  • returns false in most cases where the format of path is invalid (rather than throwing an exception)
  • returns true only if path includes the volume

In scenarios like the one the OP posed, it may therefore be more suitable than the conditions in the earlier answers. Unlike the above condition:

  • path == System.IO.Path.GetFullPath(path) throws exceptions rather than returning false in these scenarios:
    • The caller does not have the required permissions
    • The system could not retrieve the absolute path
    • path contains a colon (":") that is not part of a volume identifier
    • The specified path, file name, or both exceed the system-defined maximum length
  • System.IO.Path.IsPathRooted(path) returns true if path begins with a single directory separator.

Finally, here is a method that wraps the above condition and also forecloses the remaining possible exceptions:

public static bool IsFullPath(string path) {
    return !String.IsNullOrWhiteSpace(path)
        && path.IndexOfAny(System.IO.Path.GetInvalidPathChars().ToArray()) == -1
        && Path.IsPathRooted(path)
        && !Path.GetPathRoot(path).Equals(Path.DirectorySeparatorChar.ToString(), StringComparison.Ordinal);
}

EDIT: EM0 made a good comment and alternative answer addressing the curious case of paths like C: and C:dir. To help decide how you may want to handle such paths, you may want to take deep dive to MSDN --> Windows desktop applications --> Develop --> Desktop technologies --> Data Access and Storage --> Local File Systems --> File Management --> About File Management --> Creating, Deleting, and Maintaining Files --> Naming Files, Paths, and Namespaces --> Fully Qualified vs. Relative Paths

For Windows API functions that manipulate files, file names can often be relative to the current directory, while some APIs require a fully qualified path. A file name is relative to the current directory if it does not begin with one of the following:

  • A UNC name of any format, which always start with two backslash characters ("\"). For more information, see the next section.
  • A disk designator with a backslash, for example "C:\" or "d:\".
  • A single backslash, for example, "\directory" or "\file.txt". This is also referred to as an absolute path.

If a file name begins with only a disk designator but not the backslash after the colon, it is interpreted as a relative path to the current directory on the drive with the specified letter. Note that the current directory may or may not be the root directory depending on what it was set to during the most recent "change directory" operation on that disk. Examples of this format are as follows:

  • "C:tmp.txt" refers to a file named "tmp.txt" in the current directory on drive C.
  • "C:tempdir\tmp.txt" refers to a file in a subdirectory to the current directory on drive C.

[...]

2
  • 3
    I like that this doesn't throw for invalid paths, but it returns true for paths like "C:" and "C:dir", which are resolved by GetFullPath using the current directory (so they're not absolute). Posted an answer that returns false for these.
    – EM0
    Nov 30, 2017 at 9:26
  • @EM0 - Thanks! You just taught me something. :)
    – weir
    Dec 7, 2017 at 20:49
18

Building on weir's answer: this does not throw for invalid paths, but also returns false for paths like "C:", "C:dirname" and "\path".

public static bool IsFullPath(string path)
{
    if (string.IsNullOrWhiteSpace(path) || path.IndexOfAny(Path.GetInvalidPathChars()) != -1 || !Path.IsPathRooted(path))
        return false;
    
    string pathRoot = Path.GetPathRoot(path);
    if (pathRoot.Length <= 2 && pathRoot != "/") // Accepts X:\ and \\UNC\PATH, rejects empty string, \ and X:, but accepts / to support Linux
        return false;

    if (pathRoot[0] != '\\' || pathRoot[1] != '\\')
        return true; // Rooted and not a UNC path

    return pathRoot.Trim('\\').IndexOf('\\') != -1; // A UNC server name without a share name (e.g "\\NAME" or "\\NAME\") is invalid
}

Note that this returns different results on Windows and Linux, e.g. "/path" is absolute on Linux, but not on Windows.

Unit test:

[Test]
public void IsFullPath()
{
    bool isWindows = Environment.OSVersion.Platform.ToString().StartsWith("Win"); // .NET Framework
    // bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(OSPlatform.Windows); // .NET Core

    // These are full paths on Windows, but not on Linux
    TryIsFullPath(@"C:\dir\file.ext", isWindows);
    TryIsFullPath(@"C:\dir\", isWindows);
    TryIsFullPath(@"C:\dir", isWindows);
    TryIsFullPath(@"C:\", isWindows);
    TryIsFullPath(@"\\unc\share\dir\file.ext", isWindows);
    TryIsFullPath(@"\\unc\share", isWindows);

    // These are full paths on Linux, but not on Windows
    TryIsFullPath(@"/some/file", !isWindows);
    TryIsFullPath(@"/dir", !isWindows);
    TryIsFullPath(@"/", !isWindows);

    // Not full paths on either Windows or Linux
    TryIsFullPath(@"file.ext", false);
    TryIsFullPath(@"dir\file.ext", false);
    TryIsFullPath(@"\dir\file.ext", false);
    TryIsFullPath(@"C:", false);
    TryIsFullPath(@"C:dir\file.ext", false);
    TryIsFullPath(@"\dir", false); // An "absolute", but not "full" path

    // Invalid on both Windows and Linux
    TryIsFullPath(null, false, false);
    TryIsFullPath("", false, false);
    TryIsFullPath("   ", false, false); // technically, a valid filename on Linux

    // Invalid on Windows, valid (but not full paths) on Linux
    TryIsFullPath(@"C:\inval|d", false, !isWindows);
    TryIsFullPath(@"\\is_this_a_dir_or_a_hostname", false, !isWindows);
    TryIsFullPath(@"\\is_this_a_dir_or_a_hostname\", false, !isWindows);
    TryIsFullPath(@"\\is_this_a_dir_or_a_hostname\\", false, !isWindows);
}

private static void TryIsFullPath(string path, bool expectedIsFull, bool expectedIsValid = true)
{
    Assert.AreEqual(expectedIsFull, PathUtils.IsFullPath(path), "IsFullPath('" + path + "')");

    if (expectedIsFull)
    {
        Assert.AreEqual(path, Path.GetFullPath(path));
    }
    else if (expectedIsValid)
    {
        Assert.AreNotEqual(path, Path.GetFullPath(path));
    }
    else
    {
        Assert.That(() => Path.GetFullPath(path), Throws.Exception);
    }
}
5
  • 1
    Good stuff. I did notice that msdn.microsoft.com/en-us/library/windows/desktop/… states that on Windows a path is not relative if it begins with 'A single backslash, for example, "\directory" or "\file.txt". This is also referred to as an absolute path.'
    – weir
    Dec 7, 2017 at 21:17
  • 1
    Good point! Looks like my terminology was off. When I said "absolute path" I was really thinking of what MS calls a "full path". I've changed the name and added a test case for this.
    – EM0
    Dec 9, 2017 at 21:25
  • 1
    Thanks for this answer, it helped me a lot. However, note that for a UNC path such as \\server\, the method returns true, but this will throw an exception if you then call Directory.Exists(path) (System.ArgumentException: 'The UNC path should be of the form \\server\share.')
    – Carl
    Aug 23, 2019 at 10:51
  • 3
    Nice to see people still using this and finding new edge cases @Carl Updated the code and test for that!
    – EM0
    Aug 23, 2019 at 13:52
  • On Linux, almost anything is allowed as filename character, including the pipe and whitespace-only. This means, that @"C:\inval|d" is allowed, as well as @"\\is_this_a_dir_or_a_hostname". Even a filename consisting of spaces only (such as " ") is a valid filename (or directory name). However, for some reason .NET throws exceptions when passing such a string to Path.GetFullPath(" "), or for example File.WriteAllBytes(" ", ...), which is possibly a bug.
    – Wolfram
    May 20, 2022 at 14:38
17

On .NET Core 2.1+ and .NET

You can use Path.IsPathFullyQualified (source).

On .NET Framework

You can implement the definition of fully qualified (MS Docs). For example (on Windows):

public static bool IsPathFullyQualified(string path)
{
    var root = Path.GetPathRoot(path);
    return root.StartsWith(@"\\") || root.EndsWith(@"\") && root != @"\";
}

Note the guard against being rooted only by directory and not also by drive. See Path.IsPathFullyQualified Remarks for more explanation.

1
  • Path.IsPathFullyQualified is good, handles tricky cases like C:
    – demonplus
    Aug 2, 2022 at 8:39
14

Try

System.IO.Path.IsPathRooted(template)

Works for UNC paths as well as local ones.

E.g.

Path.IsPathRooted(@"\\MyServer\MyShare\MyDirectory")  // returns true
Path.IsPathRooted(@"C:\\MyDirectory")  // returns true
1
  • 1
    This will consider "\\myfolder" as rooted. Jun 2, 2022 at 9:44
13

Old question, but one more applicable answer. If you need to ensure the volume is included in a local path, you can use System.IO.Path.GetFullPath() like this:

if (template == System.IO.Path.GetFullPath(template))
{
    ; //template is full path including volume or full UNC path
}
else
{
    if (useCurrentPathAndVolume)
        template = System.IO.Path.GetFullPath(template);
    else
        template = Assembly.GetExecutingAssembly().Location
}
2
  • 3
    This was what I needed, and seems closer to the original question since IsPathRooted' returns true for relative paths (not necessarily absolute paths)
    – bitcoder
    Dec 7, 2015 at 20:37
  • 1
    GetFullPath accesses the file system and can throw a number of possible exceptions. See my answer (stackoverflow.com/a/35046453/704808) for an alternative that still ensures a full path.
    – weir
    Jan 27, 2016 at 20:49
8

As of .NET Core 2.1/NET Standard 2.1, you can call the following method:

Path.IsPathFullyQualified(@"c:\foo")

MSDN doc: Path.IsPathFullyQualified Method

The useful cite from MSDN doc follows:

This method handles paths that use the alternate directory separator. It's a frequent mistake to assume that rooted paths (IsPathRooted(String)) aren't relative. For example, "C:a" is drive relative, that is, it's resolved against the current directory for C: (rooted, but relative). "C:\a" is rooted and not relative, that is, the current directory isn't used to modify the path.

6

This is the solution I use

public static bool IsFullPath(string path)
{
    try
    {
        return Path.GetFullPath(path) == path;
    }
    catch
    {
        return false;
    }
}

It works the following way:

IsFullPath(@"c:\foo"); // true
IsFullPath(@"C:\foo"); // true
IsFullPath(@"c:\foo\"); // true
IsFullPath(@"c:/foo"); // false
IsFullPath(@"\foo"); // false
IsFullPath(@"foo"); // false
IsFullPath(@"c:1\foo\"); // false
2
  • Very interesting! It's fragile, for instance, has to match slash types, but this has promise. Nov 16, 2018 at 13:53
  • 2
    It returns wrong results for the following paths: C:\foo\..\foo or C:\foo\.\.\.
    – sergtk
    Oct 5, 2019 at 18:04
-1

I'm not really sure what you mean by full path (though assuming from the example you mean non-relative from the root onwards), well, you can use the Path class to aid you in working with physical filesystem paths, which should cover you for most eventualities.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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