The easiest way to check if a path is an UNC path is of course to check if the first character in the full path is a letter or backslash. Is this a good solution or could there be problems with it?
My specific problem is that I want to create an System.IO.DriveInfo-object if there is a drive letter in the path.
A UNC path uses double slashes or backslashes to precede the name of the computer. The path (disk and directories) within the computer are separated with a single slash or backslash, as in the following examples. Note that in the DOS/Windows example, drive letters (c:, d:, etc.) are not used in UNC names.
How's this for a quick and dirty way to check - run the windows net use command and parse the output for the line with the network path of interest (e.g. \\vault2 ) and OK . Here's an example of the output: C:\>net use New connections will be remembered.
The UNC path is the location of the shared folders you want to connect to. For example: "\\testserver\share\test" tells the computer that's the shared folder you want to connect to on the network drive you specified in the Drive letter drop-down menu.
UNC Name Syntax These names consist of three parts: a host device name, a share name, and an optional file path.
Try this extension method:
public static bool IsUncPath(this string path) { return Uri.TryCreate(path, UriKind.Absolute, out Uri uri) && uri.IsUnc; }
Since a path without two backslashes in the first and second positions is, by definiton, not a UNC path, this is a safe way to make this determination.
A path with a drive letter in the first position (c:) is a rooted local path.
A path without either of this things (myfolder\blah) is a relative local path. This includes a path with only a single slash (\myfolder\blah).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With