If you create an instance of DirectoryInfo
for two paths, its FullName
property should return a fully qualified canonical path. Therefore, if you just do it for both sides that you want to compare, you can do this:
if (chosenDirectory.FullName != configuredDirectory.FullName) { throw new InvalidOperationException( String.Format("Invalid path {0}.", chosenDirectory)); }
Since FullName
is just a string, you can perform regular string comparisons along paths, for example:
if (!chosenDirectory.FullName.StartsWith(configuredDirectory.FullName, StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException( String.Format("Invalid path {0}.", chosenDirectory)); }
You can also use the Parent
property and compare its FullName
with the selected directory if you do not want to allow subdirectories in the configured directory:
if (!chosenDirectory.Parent.FullName.Equals(configuredDirectory.FullName, StringComparison.InvariantCultureIgnoreCase)) { throw new InvalidOperationException( String.Format("Invalid path {0}.", chosenDirectory)); }
source share