Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / January 2007

Tip: Looking for answers? Try searching our database.

DirectoryInfo - How To Get Name in Proper Case?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff Gaines - 27 Jan 2007 13:20 GMT
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.

Signature

Jeff Gaines

Oliver Sturm - 27 Jan 2007 20:48 GMT
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
Signature

http://www.sturmnet.org/blog

Jeff Gaines - 27 Jan 2007 23:08 GMT
On 27/01/2007 in message <xn0f1puzd2mzdmo00d@msnews.microsoft.com> Oliver
Sturm wrote:

>Hello Jeff,
>
[quoted text clipped - 6 lines]
>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!

Signature

Jeff Gaines

Oliver Sturm - 28 Jan 2007 13:07 GMT
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
Signature

http://www.sturmnet.org/blog

Jeff Gaines - 28 Jan 2007 15:06 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.