Hello Jeff,
>I wrote this:
I looked into this a bit and came up with the following two methods. I
hope you still find them useful. I've added quite a lot of comments to
make things clear.
public static string MyGetCasedFolderPath(string origPath) {
DirectoryInfo origInfo = new DirectoryInfo(origPath);
// This shouldn't really be there, as it doesn't have
// anything to do with the purpose the name of this
// method communicates.
if (!origInfo.Exists)
return null;
return MyGetCasedFolderPath(origInfo).FullName;
}
public static DirectoryInfo MyGetCasedFolderPath(DirectoryInfo info) {
string prefix;
if (info.Parent == null)
// Always upper case the drive letter
return new DirectoryInfo(info.FullName.ToUpper( ));
else
prefix = MyGetCasedFolderPath(info.Parent).FullName;
// We use a search expression, which reduces the number of items
// that are being found, while still returning the correctly cased name.
// I'm making the assumption here that this is more efficient than
// comparing:
// if (dirInfo.Name.Equals(info.Name,
StringComparison.CurrentCultureIgnoreCase))
// It would probably be useful to test whether this assumption is true.
foreach (DirectoryInfo dirInfo in info.Parent.GetDirectories(info.Name,
SearchOption.TopDirectoryOnly))
return new DirectoryInfo(Path.Combine(prefix, dirInfo.Name));
// If we get here, there's something wrong
throw new Exception(String.Format("DirectoryInfo {0} doesn't exist in
its own parent.", info.FullName));
}
Oliver Sturm
--
http://www.sturmnet.org/blog