Link to home
Start Free TrialLog in
Avatar of RichieHindle
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).
Avatar of bmilli
bmilli


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)

sorry... C:\Path\File.Txt after the call.
Avatar of RichieHindle

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\MyFileWithAMixedCaseName.txt, that's the pathname I want.
Avatar of Bob Learned
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);

Open in new window

SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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\inDex.hTMl
jaime's and I believe Bob's does work with any legal length path name.
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\wOrkflow.hTMl";
"C:\\sRc\\wEb\\cOntent\\wOrkflow.hTMl"
Directory.GetFiles(Path.GetDirectoryName(fn), Path.GetFileName(fn))[0];
"C:\\sRc\\wEb\\cOntent\\workflow.html"
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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks!  I love the fact that it uppercases the drive name - nice touch. 8-)