RichieHindle
asked on
Get canonical filename - turn C:\diR\fiLe.Txt into C:\Dir\File.txt
In C#, I need to turn a full pathname which is valid but possibly of the wrong case into the "correct" pathname. So for example, there's a file called C:\Dir\File.txt but the pathname I have is C:\diR\fiLe.Txt. (I know the "wrong" pathname will work, but I still need the right one, as displayed by Windows Explorer).
sorry... C:\Path\File.Txt after the call.
ASKER
bmilli: Sorry, I didn't make myself clear. I want the true pathname, in whatever case it is on the disk. So if the file is called C:\MyDirectory\MyFileWithA MixedCaseN ame.txt, that's the pathname I want.
Try some System.IO magic with the Path and Directory classes (just for fun) :D
using System.IO;
string filePath = @"C:\TeMP\REFUNDS.CSV";
string directoryName = Path.GetDirectoryName(filePath);
string fileName = Path.GetFileName(filePath);
string realDirectory = Directory.GetDirectories(Path.GetDirectoryName(directoryName), Path.GetFileName(directoryName))[0];
string realFileName = Path.GetFileName(Directory.GetFiles(directoryName, fileName)[0]);
string realPath = Path.Combine(realDirectory, realFileName);
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Does your solution need to be cross platform? All of these solutions rely on the windows file system and its case insensitivty. Most would fail on another platform (like through mono).
Cheers,
Greg
Cheers,
Greg
ASKER
gregoryyoung: It only needs to work on Windows.
jaime_olivares / TheLearnedOne: It needs to work with any length of pathname, eg.: C:\proJECTs\mYProjecT\wEb\ cOntent\in Dex.hTMl
jaime_olivares / TheLearnedOne: It needs to work with any length of pathname, eg.: C:\proJECTs\mYProjecT\wEb\
jaime's and I believe Bob's does work with any legal length path name.
ASKER
gregoryyoung: jaime's code doesn't correct the case of the directory names, only the filename. Here's a transcript from Visual Studio's Immediate Window:
string fn = @"C:\sRc\wEb\cOntent\wOrkf low.hTMl";
"C:\\sRc\\wEb\\cOntent\\wO rkflow.hTM l"
Directory.GetFiles(Path.Ge tDirectory Name(fn), Path.GetFileName(fn))[0];
"C:\\sRc\\wEb\\cOntent\\wo rkflow.htm l"
string fn = @"C:\sRc\wEb\cOntent\wOrkf
"C:\\sRc\\wEb\\cOntent\\wO
Directory.GetFiles(Path.Ge
"C:\\sRc\\wEb\\cOntent\\wo
oh when you said "work with any length of path name" I took it to mean any level of path name ...
to get the appropriate directory name you can just use the DirectoryInfo class
Cheers,
Greg
to get the appropriate directory name you can just use the DirectoryInfo class
Cheers,
Greg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks! I love the fact that it uppercases the drive name - nice touch. 8-)
This works in VB.Net. Not sure what you would need to do other than add ; at the end for C#. sVal contains c:\patH\fiLe.txt before the call and C:\Path\Ffile.Txt after the call.
sVal = StrConv(sVal, VbStrConv.ProperCase)