Get the proper path name, proper case I mean.
-
I'm looking for a way to get the proper path name, for example if I have: "c:\\program files" I would expect "C:\\Program Files". Any ideas? I'm using the code below now, but the use of GetFileSystemInfos is going to be a performance problem some time...
private string fixPathName(string path) { string[] pathParts = path.Split(Path.DirectorySeparatorChar); string fixedPath = null; for (int index = 0; index < pathParts.Length; index++) { string pathPart = pathParts[index]; if (index == 0) { fixedPath = pathPart; fixedPath = Path.GetFullPath(fixedPath); } else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath)); string pathProperName = paths[0].FullName; fixedPath = pathProperName; } } return fixedPath; }
-
I'm looking for a way to get the proper path name, for example if I have: "c:\\program files" I would expect "C:\\Program Files". Any ideas? I'm using the code below now, but the use of GetFileSystemInfos is going to be a performance problem some time...
private string fixPathName(string path) { string[] pathParts = path.Split(Path.DirectorySeparatorChar); string fixedPath = null; for (int index = 0; index < pathParts.Length; index++) { string pathPart = pathParts[index]; if (index == 0) { fixedPath = pathPart; fixedPath = Path.GetFullPath(fixedPath); } else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath)); string pathProperName = paths[0].FullName; fixedPath = pathProperName; } } return fixedPath; }
There is no "proper name", since path specifications are not case sensitive. So, it begs the question, why would you care about the case of the characters??
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
There is no "proper name", since path specifications are not case sensitive. So, it begs the question, why would you care about the case of the characters??
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Well, so it looks right to the user. I'm working on a bit of autocomplete code (like the Run dialog). I do know about ComboBox/TextBox autocomplete, but it doesn't meet the specs.
-
I'm looking for a way to get the proper path name, for example if I have: "c:\\program files" I would expect "C:\\Program Files". Any ideas? I'm using the code below now, but the use of GetFileSystemInfos is going to be a performance problem some time...
private string fixPathName(string path) { string[] pathParts = path.Split(Path.DirectorySeparatorChar); string fixedPath = null; for (int index = 0; index < pathParts.Length; index++) { string pathPart = pathParts[index]; if (index == 0) { fixedPath = pathPart; fixedPath = Path.GetFullPath(fixedPath); } else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath)); string pathProperName = paths[0].FullName; fixedPath = pathProperName; } } return fixedPath; }
AndrewVos wrote:
else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath));
If I read this correctly, you can replace all this sutff with this:
FileInfo fileObject = new FileInfo(fixedPath); fixedPath = fileObject.FullName;
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
AndrewVos wrote:
else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath));
If I read this correctly, you can replace all this sutff with this:
FileInfo fileObject = new FileInfo(fixedPath); fixedPath = fileObject.FullName;
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008string fixedPath = @"c:\program files\"; FileInfo fileObject = new FileInfo(fixedPath); fixedPath = fileObject.FullName; MessageBox.Show(fixedPath);
This would return "c:\program files".
-
AndrewVos wrote:
else { fixedPath = Path.Combine(fixedPath, pathPart); DirectoryInfo parent = Directory.GetParent(fixedPath); FileSystemInfo[] paths = parent.GetFileSystemInfos(Path.GetFileName(fixedPath));
If I read this correctly, you can replace all this sutff with this:
FileInfo fileObject = new FileInfo(fixedPath); fixedPath = fileObject.FullName;
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Ok, I've got everything working. It seems GetDirectories is actually fast enough when using a search pattern. Thanks for reading through the code, and here's what I'm using now.
public List Generate(string generationString) { List results = new List(); string[] pathParts = generationString.Split(Path.DirectorySeparatorChar); DirectoryInfo currentDirectory = null; for (int index = 0; index < pathParts.Length;index++ ) { string pathPart = pathParts[index]; if (index == 0) { //The first part will be the drive. DriveInfo[] drives = DriveInfo.GetDrives(); foreach (DriveInfo drive in drives) { if (drive.Name.StartsWith(pathPart, StringComparison.InvariantCultureIgnoreCase)) { currentDirectory = drive.RootDirectory; } } if (currentDirectory == null) { break; } } else if ((index == pathParts.Length - 1) ) { //The last part could be anything, so we do a wildcard search. try { results.AddRange(Directory.GetDirectories(currentDirectory.FullName, pathPart + "*")); results.AddRange(Directory.GetFiles(currentDirectory.FullName, pathPart + "*")); } catch { } } else { //Just add the first result to the path. DirectoryInfo[] searchResults = currentDirectory.GetDirectories(pathPart); if (searchResults.Length == 0) { break; } else { currentDirectory = searchResults[0]; } } } return results; }