|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
DirectoryInfo - How To Get Name in Proper Case?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. -- 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. Oliver Sturm On 27/01/2007 in message <xn0f1puzd2mzdmo***@msnews.microsoft.com> Oliver
Sturm wrote: >Hello Jeff, OK, thanks Oliver :-)> >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. 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! -- Jeff Gaines Hello Jeff,
>I'll either polish my current method or have a look at the API and then If you look into the details and have further problems, please post again >put it in the library before I forget it! - I'd really like to hear that this problem has been solved. Oliver Sturm On 28/01/2007 in message <xn0f1qxgw3ly2ad***@msnews.microsoft.com> Oliver
Sturm wrote: >Hello Jeff, I wrote this:> >>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. /// <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. -- Jeff Gaines 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
Other interesting topics
Non-proportional (non Rectangular) resizing
Asynchronous Programming Model - how to implement? ComboBox updating data source via look-up table How to Create WinForms client and ClassLib server as one assembly? Disable a text box when it's not adding new row? Possible help for Newbie Studio 2005 and the ".Designer" file...?!? Code practice in properties. VS 2003, false data concurrency error Dataset and scalability issue vs 2003 to vs 2005 conversion problem |
|||||||||||||||||||||||