Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if full path given

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?

like image 794
hs2d Avatar asked Apr 06 '11 10:04

hs2d


People also ask

How do I know if my path is absolute?

js, we can use the isAbsolute method from the path module. to check if myPath is an absolute path with path. isAbsolute . It should return true since it's an absolute path.

How do I get a full file path?

Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).

How do I get the full path in Python?

Use abspath() to Get the Absolute Path in Pythonpath property. To get the absolute path using this module, call path. abspath() with the given path to get the absolute path. The output of the abspath() function will return a string value of the absolute path relative to the current working directory.

What is a fully qualified path?

A fully qualified path or absolute path always defines an exact path from a particular drive or device to a target file or directory, and does not depend on the current drive or current directory.


2 Answers

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" 
like image 173
detaylor Avatar answered Sep 21 '22 18:09

detaylor


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.

[...]

like image 25
weir Avatar answered Sep 21 '22 18:09

weir