[1.0/2.0/3.0] Obtaining Path Capitalisation
Lets say that i've got a path (or file): "D:\Games\WhaTeVer"
I want to get from an uncapitalised string such as "d:\games\whatever" to the Capitalised version (i.e. the actual file\folder).
Anyone know how to do this. I've had a look at the Path, Directory and DirectoryAttribute classes, but unless i'm missing something they don't seem to enforce the correct capitalisation much. (things like DirectoryAttributeObject.FullName simply returns the string used to create it, whether it has the actual file/folder capitalisation correct).
Thanks for any help.
Re: [1.0/2.0/3.0] Obtaining Path Capitalisation
Given that file system paths are case-insensitive, may I ask why this is important?
Re: [1.0/2.0/3.0] Obtaining Path Capitalisation
You would have to capitalize each manually via code as there is no special function for it.
Just split the path into an array and format the elements with proper casing. Then loop through your array to rebuild a string var with the original path but with the proper casing you desire.
Ps, Im taking this is just for presentation effects.
Re: [1.0/2.0/3.0] Obtaining Path Capitalisation
Yeah, it's for presentation.
RobDog, thanks for the help, but i'm not sure how i can format the elements with the proper casing.
The only way of doing this that i can think of is enumerating all folders for every directory in my path, getting the correct case for each folder as i go.
i.e. enumerate all folders in D:\, then searching for 'games' (case insensitive) within that list of folders, matching with Games, then enumerating all folders in D:\Games, then searching for 'whatever', matching with WhaTeVer and so on.
That just seems like i'm getting more information than needed (all the enumerated files). I'd hoped there was a better way, using less calls to IO functions.
Re: [1.0/2.0/3.0] Obtaining Path Capitalisation
You could start with your full path, use Path.GetDirectoryName to get the parent folder, then just get the subfolders of that.
Re: [1.0/2.0/3.0] Obtaining Path Capitalisation
Not sure how you are needing things but it sounds like your doing a recursive directory search and building your path that way? I was under the impression you already had the path and needed simple proper casing.
Re: [1.0/2.0/3.0] Obtaining Path Capitalisation
I do already have the path, but i don't have the case sensitive version. What i have is a registry entry, which happens to be all lower case. I want to change it to be the correct case.
The recursive method i described is one method of working out the correct case path, but i'm looking for something that takes less system calls.
EDIT: Thanks jmcilhinney, just saw your post, i'll give that a go and report back.
EDIT#2: Unfortunatly that doesn't work, the GetDirectoryName Method doesn't return the correct case, it keeps the case of the path you give it as an argument.
Re: [1.0/2.0/3.0] Obtaining Path Capitalisation
Quote:
Originally Posted by jmcilhinney
You could start with your full path, use Path.GetDirectoryName to get the parent folder, then just get the subfolders of that.
I tell a lie. That doesn't work.
Re: [1.0/2.0/3.0] Obtaining Path Capitalisation
Yeah, this one's really bugging me. I might just end up doing the recursive method even though it seems horribly in-efficient.
Still very much open to suggestions though! :)
Re: [1.0/2.0/3.0] Obtaining Path Capitalisation
This is a refinement of what you suggested:
vb.net Code:
Private Function GetOriginalPath(ByVal path As String) As String
Dim root As String = IO.Path.GetPathRoot(path)
Dim nodes As New Stack(Of String)
While path <> root
nodes.Push(IO.Path.GetFileName(path))
path = IO.Path.GetDirectoryName(path)
End While
Dim node As String
Dim originalPath As String = root.ToUpper()
While nodes.Count > 0
node = nodes.Pop
originalPath = IO.Directory.GetDirectories(originalPath, node)(0)
End While
Return originalPath
End Function
I forgot we were in C# land. If you need me to convert let me know.
Re: [1.0/2.0/3.0] Obtaining Path Capitalisation
Works great thanks, i had to make a few little changes to deal with paths with a trailing \ and to deal with files but other than that it works great, thanks!
Reputation++ EDIT: Well i would do, but it says i need to spread my reputation around a bit more!
In case anyone needs it, here's the code i've got now:
Code:
private static string GetRealPath(string path)
{
string Root = Path.GetPathRoot(path);
Stack<string> Nodes = new Stack<string>();
string Node;
while (string.Compare(path, Root, StringComparison.OrdinalIgnoreCase) != 0)
{
Node = Path.GetFileName(path);
if(Node != "")
Nodes.Push(Node);
path = Path.GetDirectoryName(path);
}
string RealPath = Root.ToUpper();
while (Nodes.Count > 0)
{
Node = Nodes.Pop();
string[] Directories = Directory.GetDirectories(RealPath, Node, SearchOption.TopDirectoryOnly);
if (Directories.Length == 0)
RealPath = Directory.GetFiles(RealPath, Node,SearchOption.TopDirectoryOnly)[0];
else
RealPath = Directories[0];
}
return RealPath;
}