DirectoryInfo - How To Get Name in Proper Case?

J

Jeff Gaines

I am writing an app where I want to save a directory path, provided it
exists.
If a user enters a path 'c:\temp' I can set up a new DirectoryInfo and
check it exists. Unfortunately the data inside the DirectoryInfo returns
the path as 'c:\temp' whereas it is actually 'C:\Temp'.
Case insensitivity is useful in checking for the existence of a directory
but I do want to save the properly cased name.

Any thoughts on how to achieve this? I can create another DirectoryInfo
from the parent of the first and this produces properly cased names but
then I need to iterate thought it to match the name I am looking for.
 
O

Oliver Sturm

Hello Jeff,

I don't think there's any way of querying the "real" case of a certain
file or directory directly. I've searched around a bit and the only thing
I could find was the suggestion to go the way you already found, using a
complete or partial listing of the parent directory to find the real name
of the entry. Maybe you could use an API function to find files with a
wildcard that you dynamically construct from part of the name you have?
That would at least keep the overhead down by reducing the risk of having
to iterate through large numbers of files and folders.


Oliver Sturm
 
J

Jeff Gaines

Hello Jeff,

I don't think there's any way of querying the "real" case of a certain
file or directory directly. I've searched around a bit and the only thing
I could find was the suggestion to go the way you already found, using a
complete or partial listing of the parent directory to find the real name
of the entry. Maybe you could use an API function to find files with a
wildcard that you dynamically construct from part of the name you have?
That would at least keep the overhead down by reducing the risk of having
to iterate through large numbers of files and folders.

OK, thanks Oliver :)

I'll either polish my current method or have a look at the API and then
put it in the library before I forget it!
 
O

Oliver Sturm

Hello Jeff,
I'll either polish my current method or have a look at the API and then
put it in the library before I forget it!

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.


Oliver Sturm
 
J

Jeff Gaines

Hello Jeff,


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.
 
O

Oliver Sturm

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top