Possible virtual paths:
/folder1/folder2/image.jpg
~/folder1/folder2/image.jpg
folder1/folder2/image.jpg
Concrete path:
C:\folder1\folder2\image.jpg
D:\folder1\folder2\image.jpg
C:/folder1/folder2/image.jpg
C:/folder1\folder2/image.jpg
How do you check whether a path is virtual or not in a way that's not prone to failure? The reason why I'm asking is because when I use Server.MapPath()
on a concrete path, it will throw an exception. However, what I'm passing to Server.MapPath()
can be any one of the examples I provided above and I don't know what it is before run-time.
In mathematics, the tilde often represents approximation, especially when used in duplicate, and is sometimes called the "equivalency sign." In regular expressions, the tilde is used as an operator in pattern matching, and in C programming, it is used as a bitwise operator representing a unary negation (i.e., "bitwise ...
The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.
&& This is the AND operator in C programming language. It performs logical conjunction of two expressions. ( If both expressions evaluate to True, then the result is True.
This works well enough for me:
protected string GetPath(string path)
{
if (Path.IsPathRooted(path))
{
return path;
}
return Server.MapPath(path);
}
Would Path.GetFullPath(string path)
fit your needs? You could use that method then compare if the path changed.
if (path == Path.GetFullPath(path))
{
// This is the full path (no changes)
}
else
{
// This is not the full path i.e. 'virtual' (changes)
}
Reference: http://msdn.microsoft.com/en-us/library/system.io.path.getfullpath.aspx
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