
Signature
http://www.sturmnet.org/blog
On 28/01/2007 in message <xn0f1qxgw3ly2ad00p@msnews.microsoft.com> Oliver
Sturm wrote:
>Hello Jeff,
>
[quoted text clipped - 3 lines]
>If you look into the details and have further problems, please post again
>- I'd really like to hear that this problem has been solved.
I wrote this:
/// <summary>
/// Convert a Path to the Proper Case Shown by the FileSystem
/// </summary>
/// <param name="strPathIn">Path To Convert</param>
/// <returns>Properly Cased Path</returns>
public static string GetCasedFolderPath(string strPathIn)
{
DirectoryInfo diTop = new DirectoryInfo(strPathIn);
if (!diTop.Exists)
return "";
// OK Directory Is Valid
string strReturnPath = "";
string[] strPaths = strPathIn.Split('\\');
DriveInfo drInfo = new DriveInfo(strPaths[0]);
// Drive letters always Upper Case
strReturnPath = drInfo.RootDirectory.ToString().ToUpper();
DirectoryInfo diCheck;
DirectoryInfo[] diArray;
string strCheck;
int intCount = 1;
do
{
diCheck = new DirectoryInfo(strReturnPath);
diArray = diCheck.GetDirectories();
foreach (DirectoryInfo diCurrent in diArray)
{
strCheck = diCurrent.ToString();
if (strPaths[intCount].ToLower() == strCheck.ToLower())
{
strReturnPath = Path.Combine(strReturnPath, strCheck);
break;
}
}
intCount++;
}
while (intCount <= strPaths.GetUpperBound(0));
return strReturnPath;
}
I'm not sure it's very elegant but it works!
I probably ought to put a try/catch block at the top in case of a
malformed strPathIn.

Signature
Jeff Gaines
Oliver Sturm - 28 Jan 2007 16:02 GMT
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

Signature
http://www.sturmnet.org/blog